From 0d54b4198302b0ce26957a2fb7425ceaa9c29238 Mon Sep 17 00:00:00 2001 From: Eidolon Date: Thu, 9 Apr 2026 22:44:36 -0500 Subject: [PATCH] gl: Use same GL context for RHI and GL --- src/d_clisrv.c | 10 +- src/d_main.cpp | 40 ++++--- src/f_wipe.cpp | 31 ----- src/g_game.c | 7 +- src/hardware/hw_drv.h | 2 + src/hardware/hw_main.cpp | 2 + src/hardware/r_opengl/r_opengl.cpp | 13 +- src/hwr2/hardware_state.hpp | 1 + src/hwr2/upscale_backbuffer.cpp | 14 ++- src/hwr2/upscale_backbuffer.hpp | 1 + src/i_video.h | 3 + src/i_video_common.cpp | 54 ++++++--- src/m_misc.cpp | 97 --------------- src/m_misc.h | 4 - src/p_setup.cpp | 8 +- src/rhi/gl2/gl2_rhi.cpp | 9 +- src/sdl/hwsym_sdl.c | 1 + src/sdl/i_video.cpp | 52 ++------ src/sdl/ogl_sdl.c | 1 - src/sdl/ogl_sdl.h | 5 +- src/v_video.cpp | 183 ++++++++++++----------------- src/v_video.h | 2 + 22 files changed, 204 insertions(+), 336 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 2852b7450..7206b51d1 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -2307,15 +2307,7 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic } I_UpdateNoVsync(); // page flip or blit buffer -#ifdef HWRENDER - // Only take screenshots after drawing. - if (moviemode && rendermode == render_opengl) - M_LegacySaveFrame(); - if (rendermode == render_opengl && takescreenshot) - M_DoLegacyGLScreenShot(); -#endif - - if ((moviemode || takescreenshot) && rendermode == render_soft) + if ((moviemode || takescreenshot) && rendermode != render_none) I_CaptureVideoFrame(); S_UpdateSounds(); S_UpdateClosedCaptions(); diff --git a/src/d_main.cpp b/src/d_main.cpp index 6b7044aab..a98155301 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -570,14 +570,21 @@ static bool D_Display(bool world) memset(screens[0], 32+(timeinmap&15), vid.width * vid.height * vid.bpp); } } - - if (r_splitscreen == 2) - { - // Draw over the fourth screen so you don't have to stare at a HOM :V - V_DrawFill(viewwidth, viewheight, viewwidth, viewheight, 31|V_NOSCALESTART); - } } + if (r_splitscreen == 2) + { + // Draw over the fourth screen so you don't have to stare at a HOM :V + V_DrawFill(viewwidth, viewheight, viewwidth, viewheight, 31|V_NOSCALESTART); + } + +#ifdef HWRENDER + if (rendermode == render_opengl) + { + VID_BeginLegacyGLRenderPass(); + } +#endif + for (i = 0; i <= r_splitscreen; i++) { if (players[displayplayers[i]].mo || players[displayplayers[i]].playerstate == PST_DEAD) @@ -641,6 +648,13 @@ static bool D_Display(bool world) } } +#ifdef HWRENDER + if (rendermode == render_opengl) + { + VID_EndLegacyGLRenderPass(); + } +#endif + ps_rendercalltime = I_GetPreciseTime() - ps_rendercalltime; R_RestoreLevelInterpolators(); } @@ -687,6 +701,10 @@ static bool D_Display(bool world) { VID_DisplaySoftwareScreen(); } + if (rendermode != render_none) + { + VID_DisplayRHIPostimg(); + } if (lastdraw) { @@ -1069,15 +1087,7 @@ void D_SRB2Loop(void) ranwipe = D_Display(world); } -#ifdef HWRENDER - // Only take screenshots after drawing. - if (moviemode && rendermode == render_opengl) - M_LegacySaveFrame(); - if (rendermode == render_opengl && takescreenshot) - M_DoLegacyGLScreenShot(); -#endif - - if ((moviemode || takescreenshot) && rendermode == render_soft) + if ((moviemode || takescreenshot) && rendermode != render_none) I_CaptureVideoFrame(); // consoleplayer -> displayplayers (hear sounds from viewpoint) diff --git a/src/f_wipe.cpp b/src/f_wipe.cpp index 2b7d95d41..7562d3cad 100644 --- a/src/f_wipe.cpp +++ b/src/f_wipe.cpp @@ -35,10 +35,6 @@ #include "g_game.h" #include "st_stuff.h" -#ifdef HWRENDER -#include "hardware/hw_main.h" -#endif - #if NUMSCREENS < 5 #define NOWIPE // do not enable wipe image post processing for ARM, SH and MIPS CPUs #endif @@ -356,14 +352,6 @@ static void refresh_wipe_screen_texture(rhi::Rhi& rhi, rhi::Handle void F_WipeStartScreen(void) { #ifndef NOWIPE -#ifdef HWRENDER - if(rendermode == render_opengl) - { - HWR_StartScreenWipe(); - return; - } -#endif - rhi::Rhi* rhi = srb2::sys::get_rhi(srb2::sys::g_current_rhi); if (!rhi) @@ -392,14 +380,6 @@ void F_WipeStartScreen(void) void F_WipeEndScreen(void) { #ifndef NOWIPE -#ifdef HWRENDER - if(rendermode == render_opengl) - { - HWR_EndScreenWipe(); - return; - } -#endif - rhi::Rhi* rhi = srb2::sys::get_rhi(srb2::sys::g_current_rhi); if (!rhi) @@ -501,12 +481,6 @@ void F_RunWipe(UINT8 wipemode, UINT8 wipetype, boolean drawMenu, const char *col } lastwipetic = nowtime; -#ifdef HWRENDER - if (rendermode == render_opengl) - HWR_DoWipe(wipetype, wipeframe-1); // send in the wipe type and wipeframe because we need to cache the graphic - else -#endif - if (rendermode != render_none) //this allows F_RunWipe to be called in dedicated servers { // F_DoWipe(fmask, fcolor, reverse); @@ -563,11 +537,6 @@ void F_RunWipe(UINT8 wipemode, UINT8 wipetype, boolean drawMenu, const char *col I_FinishUpdate(); // page flip or blit buffer -#ifdef HWRENDER - if (moviemode && rendermode == render_opengl) - M_LegacySaveFrame(); - else -#endif if (moviemode && rendermode == render_soft) I_CaptureVideoFrame(); diff --git a/src/g_game.c b/src/g_game.c index ce69d80c0..61130268d 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -1304,12 +1304,7 @@ void G_PreLevelTitleCard(void) I_FinishUpdate(); // page flip or blit buffer NetKeepAlive(); // Prevent timeouts -#ifdef HWRENDER - if (moviemode && rendermode == render_opengl) - M_LegacySaveFrame(); - else -#endif - if (moviemode && rendermode == render_soft) + if (moviemode && rendermode != render_none) I_CaptureVideoFrame(); while (!((nowtime = I_GetTime()) - lasttime)) diff --git a/src/hardware/hw_drv.h b/src/hardware/hw_drv.h index c4396c8e5..da93fbffd 100644 --- a/src/hardware/hw_drv.h +++ b/src/hardware/hw_drv.h @@ -41,6 +41,7 @@ EXPORT void HWRAPI(DrawPolygon) (FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUI EXPORT void HWRAPI(DrawIndexedTriangles) (FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPts, FBITFIELD PolyFlags, UINT32 *IndexArray); EXPORT void HWRAPI(RenderSkyDome) (gl_sky_t *sky); EXPORT void HWRAPI(SetBlend) (FBITFIELD PolyFlags); +EXPORT void HWRAPI(ResetRenderState) (void); EXPORT void HWRAPI(ClearBuffer) (FBOOLEAN ColorMask, FBOOLEAN DepthMask, FRGBAFloat *ClearColor); EXPORT void HWRAPI(SetTexture) (GLMipmap_t *TexInfo); EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *TexInfo); @@ -126,6 +127,7 @@ struct hwdriver_s SetShaderInfo pfnSetShaderInfo; LoadCustomShader pfnLoadCustomShader; + ResetRenderState pfnResetRenderState; }; extern struct hwdriver_s hwdriver; diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index 59e47a755..c1a5baa24 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -5993,6 +5993,8 @@ static void HWR_RollTransform(FTransform *tr, angle_t roll) void HWR_RenderPlayerView(void) { + HWD.pfnResetRenderState(); + player_t * player = &players[displayplayers[viewssnum]]; const boolean skybox = (player->skybox.viewpoint && cv_skybox.value); // True if there's a skybox object and skyboxes are on diff --git a/src/hardware/r_opengl/r_opengl.cpp b/src/hardware/r_opengl/r_opengl.cpp index f22d56325..de017cc60 100644 --- a/src/hardware/r_opengl/r_opengl.cpp +++ b/src/hardware/r_opengl/r_opengl.cpp @@ -1532,6 +1532,8 @@ EXPORT void HWRAPI(ReadRect) (INT32 x, INT32 y, INT32 width, INT32 height, bottom -= dst_stride; } free(row); + pglPixelStorei(GL_UNPACK_ALIGNMENT, 4); + pglPixelStorei(GL_PACK_ALIGNMENT, 4); } else { @@ -1553,6 +1555,8 @@ EXPORT void HWRAPI(ReadRect) (INT32 x, INT32 y, INT32 width, INT32 height, } } free(image); + pglPixelStorei(GL_UNPACK_ALIGNMENT, 4); + pglPixelStorei(GL_PACK_ALIGNMENT, 4); } } @@ -1609,8 +1613,6 @@ EXPORT void HWRAPI(ClearBuffer) (FBOOLEAN ColorMask, SetBlend(DepthMask ? PF_Occlude | CurrentPolyFlags : CurrentPolyFlags&~PF_Occlude); pglClear(ClearMask); - pglEnableClientState(GL_VERTEX_ARRAY); // We always use this one - pglEnableClientState(GL_TEXTURE_COORD_ARRAY); // And mostly this one, too } @@ -1866,6 +1868,11 @@ EXPORT void HWRAPI(SetBlend) (FBITFIELD PolyFlags) CurrentPolyFlags = PolyFlags; } +EXPORT void HWRAPI(ResetRenderState) (void) +{ + SetStates(); +} + static void AllocTextureBuffer(GLMipmap_t *pTexInfo) { size_t size = pTexInfo->width * pTexInfo->height; @@ -2082,6 +2089,8 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo) if (maximumAnisotropy) pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropic_filter); + pglPixelStorei(GL_UNPACK_ALIGNMENT, 4); + pglPixelStorei(GL_PACK_ALIGNMENT, 4); pglActiveTexture(GL_TEXTURE0); } diff --git a/src/hwr2/hardware_state.hpp b/src/hwr2/hardware_state.hpp index 607e58a2d..97fcf0806 100644 --- a/src/hwr2/hardware_state.hpp +++ b/src/hwr2/hardware_state.hpp @@ -45,6 +45,7 @@ struct HardwareState std::unique_ptr crtsharp_blit_rect; std::unique_ptr screen_capture; std::unique_ptr backbuffer; + std::unique_ptr legacygl_backbuffer; std::unique_ptr imgui_renderer; WipeFrames wipe_frames; }; diff --git a/src/hwr2/upscale_backbuffer.cpp b/src/hwr2/upscale_backbuffer.cpp index 4984198c3..b4d5b0a67 100644 --- a/src/hwr2/upscale_backbuffer.cpp +++ b/src/hwr2/upscale_backbuffer.cpp @@ -47,17 +47,29 @@ void UpscaleBackbuffer::begin_pass(Rhi& rhi) } TextureDesc color_tex {}; - color_tex.format = TextureFormat::kRGBA; + color_tex.format = TextureFormat::kRGB; color_tex.width = vid_width; color_tex.height = vid_height; color_tex.u_wrap = TextureWrapMode::kClamp; color_tex.v_wrap = TextureWrapMode::kClamp; color_ = rhi.create_texture(color_tex); + + if (depth_) + { + rhi.destroy_renderbuffer(depth_); + depth_ = kNullHandle; + } + + RenderbufferDesc rb_desc {}; + rb_desc.width = vid_width; + rb_desc.height = vid_height; + depth_ = rhi.create_renderbuffer(rb_desc); } RenderPassBeginInfo begin_info {}; begin_info.clear_color = {0, 0, 0, 1}; begin_info.color_attachment = color_; + begin_info.depth_stencil_attachment = depth_; begin_info.color_load_op = rhi::AttachmentLoadOp::kLoad; begin_info.color_store_op = rhi::AttachmentStoreOp::kStore; begin_info.depth_load_op = rhi::AttachmentLoadOp::kLoad; diff --git a/src/hwr2/upscale_backbuffer.hpp b/src/hwr2/upscale_backbuffer.hpp index f2a4c2699..fe0569d3e 100644 --- a/src/hwr2/upscale_backbuffer.hpp +++ b/src/hwr2/upscale_backbuffer.hpp @@ -19,6 +19,7 @@ namespace srb2::hwr2 class UpscaleBackbuffer { rhi::Handle color_; + rhi::Handle depth_; public: UpscaleBackbuffer(); diff --git a/src/i_video.h b/src/i_video.h index 12150f41e..32390eabb 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -170,6 +170,9 @@ UINT32 I_GetRefreshRate(void); void I_CaptureVideoFrame(void); +void VID_BeginLegacyGLRenderPass(void); +void VID_EndLegacyGLRenderPass(void); + #ifdef __cplusplus } // extern "C" #endif diff --git a/src/i_video_common.cpp b/src/i_video_common.cpp index f52f9298e..045a551d2 100644 --- a/src/i_video_common.cpp +++ b/src/i_video_common.cpp @@ -87,6 +87,7 @@ static void reset_hardware_state(Rhi* rhi) g_hw_state.crtsharp_blit_rect = std::make_unique(BlitRectPass::BlitMode::kCrtSharp); g_hw_state.screen_capture = std::make_unique(); g_hw_state.backbuffer = std::make_unique(); + g_hw_state.legacygl_backbuffer = std::make_unique(); g_hw_state.imgui_renderer = std::make_unique(); g_hw_state.wipe_frames = {}; @@ -215,13 +216,6 @@ void I_StartDisplayUpdate(void) return; } -#ifdef HWRENDER - if (rendermode == render_opengl) - { - return; - } -#endif - rhi::Rhi* rhi = sys::get_rhi(sys::g_current_rhi); if (rhi == nullptr) @@ -252,15 +246,6 @@ void I_FinishUpdate(void) return; } -#ifdef HWRENDER - if (rendermode == render_opengl) - { - finish_legacy_ogl_update(); - FrameMark; - return; - } -#endif - temp_legacy_finishupdate_draws(); rhi::Rhi* rhi = sys::get_rhi(sys::g_current_rhi); @@ -338,3 +323,40 @@ void I_FinishUpdate(void) // Immediately prepare to begin drawing the next frame I_StartDisplayUpdate(); } + +void VID_BeginLegacyGLRenderPass(void) +{ + if (rendermode == render_none) + { + return; + } + + rhi::Rhi* rhi = sys::get_rhi(sys::g_current_rhi); + + if (rhi == nullptr) + { + // ??? + return; + } + + g_hw_state.twodee_renderer->flush(*rhi, g_2d); + g_hw_state.legacygl_backbuffer->begin_pass(*rhi); +} + +void VID_EndLegacyGLRenderPass(void) +{ + if (rendermode == render_none) + { + return; + } + + rhi::Rhi* rhi = sys::get_rhi(sys::g_current_rhi); + + if (rhi == nullptr) + { + // ??? + return; + } + + rhi->pop_render_pass(); +} diff --git a/src/m_misc.cpp b/src/m_misc.cpp index f63aa6319..453e1fcab 100644 --- a/src/m_misc.cpp +++ b/src/m_misc.cpp @@ -1415,97 +1415,6 @@ void M_StartMovie(moviemode_t mode) static void M_SaveFrame_AVRecorder(uint32_t width, uint32_t height, tcb::span data); -void M_LegacySaveFrame(void) -{ -#if NUMSCREENS > 2 - // TODO: until HWR2 replaces legacy OpenGL renderer, this - // function still needs to called for OpenGL. -#ifdef HWRENDER - if (rendermode != render_opengl) -#endif - { - return; - } - - // paranoia: should be unnecessary without singletics - static tic_t oldtic = 0; - -#ifdef SRB2_CONFIG_ENABLE_WEBM_MOVIES - if (moviemode == MM_AVRECORDER) - { - if (M_AVRecorder_IsExpired()) - { - M_StopMovie(); - return; - } - } -#endif - - // skip interpolated frames for other modes - if (oldtic == I_GetTime()) - return; - else - oldtic = I_GetTime(); - - switch (moviemode) - { - case MM_SCREENSHOT: - takescreenshot = true; - return; - case MM_GIF: - GIF_frame(); - return; - case MM_APNG: -#ifdef USE_APNG - { - UINT8 *linear = NULL; - if (!apng_FILE) // should not happen!! - { - moviemode = MM_OFF; - return; - } - - if (rendermode == render_soft) - { - // munge planar buffer to linear - linear = screens[2]; - I_ReadScreen(linear); - } -#ifdef HWRENDER - else - linear = HWR_GetScreenshot(); -#endif - M_PNGFrame(apng_ptr, apng_info_ptr, (png_bytep)linear); -#ifdef HWRENDER - if (rendermode == render_opengl && linear) - free(linear); -#endif - - if (apng_frames == PNG_UINT_31_MAX) - { - CONS_Alert(CONS_NOTICE, M_GetText("Max movie size reached\n")); - M_StopMovie(); - } - } -#else - moviemode = MM_OFF; -#endif - return; - case MM_AVRECORDER: -#if defined(SRB2_CONFIG_ENABLE_WEBM_MOVIES) && defined(HWRENDER) - { - UINT8 *linear = HWR_GetScreenshot(); - M_SaveFrame_AVRecorder(vid.width, vid.height, tcb::as_bytes(tcb::span(linear, 3 * vid.width * vid.height))); - free(linear); - } -#endif - return; - default: - return; - } -#endif -} - static void M_SaveFrame_GIF(uint32_t width, uint32_t height, tcb::span data) { if (moviemode != MM_GIF) @@ -1801,12 +1710,6 @@ void M_ScreenShot(void) takescreenshot = true; } -void M_DoLegacyGLScreenShot(void) -{ - const std::byte* fake_data = nullptr; - M_DoScreenShot(vid.width, vid.height, tcb::span(fake_data, vid.width * vid.height)); -} - /** Takes a screenshot. * The screenshot is saved as "srb2xxxx.png" where xxxx is the lowest * four-digit number for which a file does not already exist. diff --git a/src/m_misc.h b/src/m_misc.h index d31c2871b..c8b70c9a7 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -60,7 +60,6 @@ extern consvar_t cv_zlib_memorya, cv_zlib_levela, cv_zlib_strategya, cv_zlib_win extern consvar_t cv_apng_delay, cv_apng_downscale; void M_StartMovie(moviemode_t mode); -void M_LegacySaveFrame(void); void M_StopMovie(void); // the file where game vars and settings are saved @@ -108,9 +107,6 @@ boolean M_SavePNG(const char *filename, const void *data, int width, int height, extern boolean takescreenshot; void M_ScreenShot(void); -#ifdef HWRENDER -void M_DoLegacyGLScreenShot(void); -#endif void M_ScreenshotTicker(void); void M_MinimapGenerate(void); diff --git a/src/p_setup.cpp b/src/p_setup.cpp index 2644262ae..9d124d0ba 100644 --- a/src/p_setup.cpp +++ b/src/p_setup.cpp @@ -8571,9 +8571,7 @@ boolean P_LoadLevel(boolean fromnetsave, boolean reloadinggamestate) I_UpdateTime(); \ } \ lastwipetic = nowtime; \ - if (moviemode && rendermode == render_opengl) \ - M_LegacySaveFrame(); \ - else if (moviemode && rendermode == render_soft) \ + if (moviemode && rendermode != render_none) \ I_CaptureVideoFrame(); \ NetKeepAlive(); \ } \ @@ -8768,9 +8766,7 @@ boolean P_LoadLevel(boolean fromnetsave, boolean reloadinggamestate) } lastwipetic = nowtime; - if (moviemode && rendermode == render_opengl) - M_LegacySaveFrame(); - else if (moviemode && rendermode == render_soft) + if (moviemode && rendermode != render_none) I_CaptureVideoFrame(); NetKeepAlive(); } diff --git a/src/rhi/gl2/gl2_rhi.cpp b/src/rhi/gl2/gl2_rhi.cpp index cf35eebb5..18d303905 100644 --- a/src/rhi/gl2/gl2_rhi.cpp +++ b/src/rhi/gl2/gl2_rhi.cpp @@ -1024,7 +1024,7 @@ void Gl2Rhi::apply_framebuffer(const RenderPassBeginInfo& info, bool allow_clear SRB2_ASSERT(texture_slab_.is_valid(info.color_attachment)); auto& texture = texture_slab_[info.color_attachment]; - SRB2_ASSERT(texture.desc.format == TextureFormat::kRGBA); + SRB2_ASSERT(texture.desc.format == TextureFormat::kRGBA || texture.desc.format == TextureFormat::kRGB); gl_->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.texture, 0); GL_ASSERT; @@ -1432,6 +1432,13 @@ void Gl2Rhi::set_rasterizer_state(const RasterizerStateDesc& desc) } gl_->Scissor(desc.scissor.x, desc.scissor.y, desc.scissor.w, desc.scissor.h); GL_ASSERT; + + // We never use fixed func alpha test, but Legacy GL does, so it has to be turned off. + // Note that Core profile and GLES 2+ don't have this at all. + gl_->Disable(GL_ALPHA_TEST); + GL_ASSERT; + gl_->AlphaFunc(GL_ALWAYS, 0); + GL_ASSERT; } void Gl2Rhi::set_viewport(const Rect& rect) diff --git a/src/sdl/hwsym_sdl.c b/src/sdl/hwsym_sdl.c index a801c73e9..e9c73891d 100644 --- a/src/sdl/hwsym_sdl.c +++ b/src/sdl/hwsym_sdl.c @@ -107,6 +107,7 @@ void *hwSym(const char *funcName,void *handle) GETFUNC(SetShaderInfo); GETFUNC(LoadCustomShader); + GETFUNC(ResetRenderState); #else //HWRENDER if (0 == strcmp("FinishUpdate", funcName)) diff --git a/src/sdl/i_video.cpp b/src/sdl/i_video.cpp index 7fa8e0c3f..69fcb5336 100644 --- a/src/sdl/i_video.cpp +++ b/src/sdl/i_video.cpp @@ -1321,27 +1321,6 @@ static void init_imgui() static SDL_bool Impl_CreateContext(void) { -#ifdef HWRENDER - if (rendermode == render_opengl) - { - if (!g_legacy_gl_context) - { - SDL_GL_ResetAttributes(); - g_legacy_gl_context = SDL_GL_CreateContext(window); - } - if (g_legacy_gl_context == NULL) - { - SDL_DestroyWindow(window); - I_Error("Failed to create a Legacy GL context: %s\n", SDL_GetError()); - } - init_imgui(); - SDL_GL_MakeCurrent(window, g_legacy_gl_context); - return SDL_TRUE; - } -#endif - - // RHI always uses OpenGL 2.0 (for now) - if (!sdlglcontext) { SDL_GL_ResetAttributes(); @@ -1352,11 +1331,18 @@ static SDL_bool Impl_CreateContext(void) if (sdlglcontext == NULL) { SDL_DestroyWindow(window); - I_Error("Failed to create an RHI GL context: %s\n", SDL_GetError()); + I_Error("Failed to create a GL context: %s\n", SDL_GetError()); } init_imgui(); SDL_GL_MakeCurrent(window, sdlglcontext); +#ifdef HWRENDER + if (rendermode == render_opengl) + { + LoadGL(); + } +#endif + if (!g_rhi) { std::unique_ptr platform = std::make_unique(); @@ -1674,6 +1660,7 @@ void VID_StartupOpenGL(void) *(void**)&HWD.pfnSetShaderInfo = hwSym("SetShaderInfo",NULL); *(void**)&HWD.pfnLoadCustomShader = hwSym("LoadCustomShader",NULL); + *(void**)&HWD.pfnResetRenderState = hwSym("ResetRenderState",NULL); glstartup = true; } @@ -1746,24 +1733,9 @@ void srb2::cvarhandler::on_set_vid_wait() interval = 1; } - switch (rendermode) + if (sdlglcontext == nullptr || SDL_GL_GetCurrentContext() != sdlglcontext) { - case render_soft: - if (sdlglcontext == nullptr || SDL_GL_GetCurrentContext() != sdlglcontext) - { - return; - } - SDL_GL_SetSwapInterval(interval); - break; -#ifdef HWRENDER - case render_opengl: - if (g_legacy_gl_context == nullptr || SDL_GL_GetCurrentContext() != g_legacy_gl_context) - { - return; - } - SDL_GL_SetSwapInterval(interval); -#endif - default: - break; + return; } + SDL_GL_SetSwapInterval(interval); } diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 17eb1b8b3..73433b98f 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -66,7 +66,6 @@ PFNglGetString pglGetString; INT32 oglflags = 0; void *GLUhandle = NULL; SDL_GLContext sdlglcontext = 0; -SDL_GLContext g_legacy_gl_context = 0; void *GetGLFunc(const char *proc) { diff --git a/src/sdl/ogl_sdl.h b/src/sdl/ogl_sdl.h index 7eb122c49..c38cdb6a2 100644 --- a/src/sdl/ogl_sdl.h +++ b/src/sdl/ogl_sdl.h @@ -30,10 +30,13 @@ boolean OglSdlSurface(INT32 w, INT32 h); void OglSdlFinishUpdate(boolean vidwait); extern SDL_GLContext sdlglcontext; -extern SDL_GLContext g_legacy_gl_context; extern Uint16 realwidth; extern Uint16 realheight; +#ifdef HWRENDER +boolean LoadGL(void); +#endif + #ifdef _CREATE_DLL_ EXPORT void HWRAPI( OglSdlSetPalette ) (RGBA_t *palette); #endif diff --git a/src/v_video.cpp b/src/v_video.cpp index 481a70d5f..1a403cc82 100644 --- a/src/v_video.cpp +++ b/src/v_video.cpp @@ -440,15 +440,14 @@ void V_SetPalette(INT32 palettenum) } } +#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) + if (rendermode != render_none) + I_SetPalette(&pLocalPalette[palettenum*256]); +#endif #ifdef HWRENDER if (rendermode == render_opengl) HWR_SetPalette(&pLocalPalette[palettenum*256]); -#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) - else #endif -#endif - if (rendermode != render_none) - I_SetPalette(&pLocalPalette[palettenum*256]); } void V_SetPaletteLump(const char *pal) @@ -798,14 +797,6 @@ void V_DrawStretchyFixedPatch(fixed_t x, fixed_t y, fixed_t pscale, fixed_t vsca if (rendermode == render_none) return; -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_DrawStretchyFixedPatch(patch, x, y, pscale, vscale, scrn, colormap); - return; - } -#endif - if ((blendmode = ((scrn & V_BLENDMASK) >> V_BLENDSHIFT))) blendmode++; // realign to constants if ((alphalevel = V_GetAlphaLevel(scrn)) >= 10) @@ -1016,14 +1007,6 @@ void V_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 c) if (rendermode == render_none) return; -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_DrawFill(x, y, w, h, c); - return; - } -#endif - UINT32 alphalevel; if ((alphalevel = V_GetAlphaLevel(c)) >= 10) return; @@ -1139,15 +1122,6 @@ void V_DrawFillConsoleMap(INT32 x, INT32 y, INT32 w, INT32 h, INT32 c) if (rendermode == render_none) return; -#ifdef HWRENDER - if (rendermode == render_opengl) - { - UINT32 hwcolor = V_GetHWConsBackColor(); - HWR_DrawConsoleFill(x, y, w, h, c, hwcolor); // we still use the regular color stuff but only for flags. actual draw color is "hwcolor" for this. - return; - } -#endif - if ((alphalevel = V_GetAlphaLevel(c)) >= 10) return; @@ -1210,14 +1184,6 @@ void V_DrawDiag(INT32 x, INT32 y, INT32 wh, INT32 c) if (rendermode == render_none) return; -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_DrawDiag(x, y, wh, c); - return; - } -#endif - if (!(c & V_NOSCALESTART)) { INT32 dupx = vid.dupx, dupy = vid.dupy; @@ -1292,15 +1258,6 @@ void V_DrawFadeFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 c, UINT16 color, U if (rendermode == render_none) return; -#ifdef HWRENDER - if (rendermode == render_opengl) - { - // ughhhhh please can someone else do this? thanks ~toast 25/7/19 in 38 degrees centigrade w/o AC - HWR_DrawFadeFill(x, y, w, h, c, color, strength); // toast two days later - left above comment in 'cause it's funny - return; - } -#endif - if (!(c & V_NOSCALESTART)) { INT32 dupx = vid.dupx, dupy = vid.dupy; @@ -1380,13 +1337,6 @@ void V_DrawFlatFill(INT32 x, INT32 y, INT32 w, INT32 h, lumpnum_t flatnum) size_t size; size_t lflatsize; -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_DrawFlatFill(x, y, w, h, flatnum); - return; - } -#endif size = W_LumpLength(flatnum); switch (size) @@ -1521,14 +1471,6 @@ void V_DrawVhsEffect(boolean rewind) // void V_DrawFadeScreen(UINT16 color, UINT8 strength) { -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_FadeScreenMenuBack(color, strength); - return; - } -#endif - float r; float g; float b; @@ -1638,13 +1580,6 @@ void V_DrawCustomFadeScreen(const char *lump, UINT8 strength) void V_DrawFadeConsBack(INT32 plines) { UINT32 hwcolor = V_GetHWConsBackColor(); -#ifdef HWRENDER // not win32 only 19990829 by Kin - if (rendermode == render_opengl) - { - HWR_DrawConsoleBack(hwcolor, plines); - return; - } -#endif float r = ((hwcolor & 0xFF000000) >> 24) / 255.f; float g = ((hwcolor & 0xFF0000) >> 16) / 255.f; @@ -1663,14 +1598,6 @@ void V_DrawFadeConsBack(INT32 plines) // void V_EncoreInvertScreen(void) { -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_EncoreInvertScreen(); - return; - } -#endif - g_2d.begin_quad() .blend(hwr2::BlendMode::kInvertDest) .color(1, 1, 1, 1) @@ -1698,14 +1625,6 @@ void V_DrawPromptBack(INT32 boxheight, INT32 color) UINT32 hwcolor = V_GetHWConsBackColor(); -#ifdef HWRENDER - if (rendermode == render_opengl) - { - HWR_DrawTutorialBack(hwcolor, boxheight); - return; - } -#endif - float r = ((color & 0xFF000000) >> 24) / 255.f; float g = ((color & 0xFF0000) >> 16) / 255.f; float b = ((color & 0xFF00) >> 8) / 255.f; @@ -3771,13 +3690,17 @@ void VID_DisplaySoftwareScreen() { ZoneScoped; - // TODO implement - // upload framebuffer, bind pipeline, draw rhi::Rhi* rhi = srb2::sys::get_rhi(srb2::sys::g_current_rhi); hwr2::HardwareState* hw_state = srb2::sys::main_hardware_state(); // Misnomer; this just uploads the screen to the software indexed screen texture hw_state->software_screen_renderer->draw(*rhi); +} + +void VID_DisplayRHIPostimg(void) +{ + rhi::Rhi* rhi = srb2::sys::get_rhi(srb2::sys::g_current_rhi); + hwr2::HardwareState* hw_state = srb2::sys::main_hardware_state(); const int screens = std::clamp(r_splitscreen + 1, 1, MAXSPLITSCREENPLAYERS); hw_state->blit_postimg_screens->set_num_screens(screens); @@ -3791,43 +3714,91 @@ void VID_DisplaySoftwareScreen() if (screens > 2) { uv_size = glm::vec2(.5f, .5f); - switch (i) + if (rendermode == render_opengl) { - case 0: - uv_offset = glm::vec2(0.f, 0.f); - break; - case 1: - uv_offset = glm::vec2(.5f, 0.f); - break; - case 2: - uv_offset = glm::vec2(0.f, .5f); - break; - case 3: - uv_offset = glm::vec2(.5f, .5f); - break; + switch (i) + { + case 0: + uv_offset = glm::vec2(0.f, .5f); + break; + case 1: + uv_offset = glm::vec2(.5f, .5f); + break; + case 2: + uv_offset = glm::vec2(0.f, 0.f); + break; + case 3: + uv_offset = glm::vec2(.5f, 0.f); + break; + } + } + else + { + switch (i) + { + case 0: + uv_offset = glm::vec2(0.f, 0.f); + break; + case 1: + uv_offset = glm::vec2(.5f, 0.f); + break; + case 2: + uv_offset = glm::vec2(0.f, .5f); + break; + case 3: + uv_offset = glm::vec2(.5f, .5f); + break; + } } } else if (screens > 1) { uv_size = glm::vec2(1.f, .5f); - if (i == 1) + if (rendermode == render_opengl) { - uv_offset = glm::vec2(0.f, .5f); + uv_offset = glm::vec2(0.f, i == 0 ? .5f : 0.f); } + else + { + if (i == 1) + { + uv_offset = glm::vec2(0.f, .5f); + } + } + } + + rhi::Handle source; + bool indexed; + bool vflip; + bool mirror; + if (rendermode == render_soft) + { + source = hw_state->software_screen_renderer->screen(); + indexed = true; + vflip = postimgtype[i] == postimg_flip; + mirror = postimgtype[i] == postimg_mirror; + } + else + { + source = hw_state->legacygl_backbuffer->color(); + indexed = false; + // Legacy GL handles these on its own. + vflip = true; + mirror = false; } hw_state->blit_postimg_screens->set_screen( i, { - hw_state->software_screen_renderer->screen(), - true, + source, + indexed, uv_offset, uv_size, { postimgtype[i] == postimg_water && !cv_reducevfx.value, postimgtype[i] == postimg_heat && !cv_reducevfx.value, - postimgtype[i] == postimg_flip, - postimgtype[i] == postimg_mirror + vflip, + mirror } } ); diff --git a/src/v_video.h b/src/v_video.h index a41da4de5..cd57e59d9 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -442,6 +442,8 @@ void VID_BlitLinearScreen(const UINT8 *srcptr, UINT8 *destptr, INT32 width, INT3 */ void VID_DisplaySoftwareScreen(void); +void VID_DisplayRHIPostimg(void); + char *V_ParseText(const char *rawText); // Launder srb2::draw::TextElement.parse() through C code!