From c203df5edb6fde00ef6525afe5198327cd3aca73 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 24 Apr 2020 21:47:18 +0200 Subject: [PATCH] - consolidated the 3 virtually identical instances of DrawScene and moved the function to HWDrawInfo. The only backend dependent call in there, ambient occlusion, has been made a virtual of DFrameBuffer. --- src/rendering/gl/renderer/gl_renderer.h | 1 - src/rendering/gl/renderer/gl_scene.cpp | 72 +------------------ src/rendering/gl/system/gl_framebuffer.cpp | 10 +++ src/rendering/gl/system/gl_framebuffer.h | 1 + .../hwrenderer/scene/hw_drawinfo.cpp | 71 ++++++++++++++++-- src/rendering/hwrenderer/scene/hw_drawinfo.h | 5 +- src/rendering/hwrenderer/scene/hw_portal.h | 2 +- .../polyrenderer/backend/poly_framebuffer.cpp | 65 ++--------------- .../polyrenderer/backend/poly_framebuffer.h | 2 +- src/rendering/v_video.h | 1 + .../vulkan/system/vk_framebuffer.cpp | 65 ++--------------- src/rendering/vulkan/system/vk_framebuffer.h | 2 +- 12 files changed, 96 insertions(+), 201 deletions(-) diff --git a/src/rendering/gl/renderer/gl_renderer.h b/src/rendering/gl/renderer/gl_renderer.h index 3dde0195cd..84448bfc37 100644 --- a/src/rendering/gl/renderer/gl_renderer.h +++ b/src/rendering/gl/renderer/gl_renderer.h @@ -102,7 +102,6 @@ public: private: - void DrawScene(HWDrawInfo *di, int drawmode); bool QuadStereoCheckInitialRenderContextState(); void PresentAnaglyph(bool r, bool g, bool b); void PresentSideBySide(); diff --git a/src/rendering/gl/renderer/gl_scene.cpp b/src/rendering/gl/renderer/gl_scene.cpp index a1c7a9f7a8..4f99f2e2a7 100644 --- a/src/rendering/gl/renderer/gl_scene.cpp +++ b/src/rendering/gl/renderer/gl_scene.cpp @@ -65,7 +65,6 @@ // //========================================================================== CVAR(Bool, gl_texture, true, 0) -CVAR(Bool, gl_no_skyclear, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CVAR(Float, gl_mask_threshold, 0.5f,CVAR_ARCHIVE|CVAR_GLOBALCONFIG) CVAR(Float, gl_mask_sprite_threshold, 0.5f,CVAR_ARCHIVE|CVAR_GLOBALCONFIG) @@ -78,72 +77,6 @@ EXTERN_CVAR (Bool, r_drawvoxels) namespace OpenGLRenderer { -//----------------------------------------------------------------------------- -// -// gl_drawscene - this function renders the scene from the current -// viewpoint, including mirrors and skyboxes and other portals -// It is assumed that the HWPortal::EndFrame returns with the -// stencil, z-buffer and the projection matrix intact! -// -//----------------------------------------------------------------------------- - -void FGLRenderer::DrawScene(HWDrawInfo *di, int drawmode) -{ - static int recursion=0; - static int ssao_portals_available = 0; - const auto &vp = di->Viewpoint; - - bool applySSAO = false; - if (drawmode == DM_MAINVIEW) - { - ssao_portals_available = gl_ssao_portals; - applySSAO = true; - } - else if (drawmode == DM_OFFSCREEN) - { - ssao_portals_available = 0; - } - else if (drawmode == DM_PORTAL && ssao_portals_available > 0) - { - applySSAO = true; - ssao_portals_available--; - } - - if (vp.camera != nullptr) - { - ActorRenderFlags savedflags = vp.camera->renderflags; - di->CreateScene(drawmode == DM_MAINVIEW); - vp.camera->renderflags = savedflags; - } - else - { - di->CreateScene(false); - } - - glDepthMask(true); - if (!gl_no_skyclear) screen->mPortalState->RenderFirstSkyPortal(recursion, di, gl_RenderState); - - di->RenderScene(gl_RenderState); - - if (applySSAO && gl_RenderState.GetPassType() == GBUFFER_PASS) - { - gl_RenderState.EnableDrawBuffers(1); - GLRenderer->AmbientOccludeScene(di->VPUniforms.mProjectionMatrix.get()[5]); - glViewport(screen->mSceneViewport.left, screen->mSceneViewport.top, screen->mSceneViewport.width, screen->mSceneViewport.height); - GLRenderer->mBuffers->BindSceneFB(true); - gl_RenderState.EnableDrawBuffers(gl_RenderState.GetPassDrawBufferCount()); - gl_RenderState.Apply(); - screen->mViewpoints->Bind(gl_RenderState, di->vpIndex); - } - - // Handle all portals after rendering the opaque objects but before - // doing all translucent stuff - recursion++; - screen->mPortalState->EndFrame(di, gl_RenderState); - recursion--; - di->RenderTranslucent(gl_RenderState); -} - //----------------------------------------------------------------------------- // // Renders one viewpoint in a scene @@ -195,10 +128,7 @@ sector_t * FGLRenderer::RenderViewpoint (FRenderViewpoint &mainvp, AActor * came vp.Pos += eye.GetViewShift(vp.HWAngles.Yaw.Degrees); di->SetupView(gl_RenderState, vp.Pos.X, vp.Pos.Y, vp.Pos.Z, false, false); - // std::function until this can be done better in a cross-API fashion. - di->ProcessScene(toscreen, [&](HWDrawInfo *di, int mode) { - DrawScene(di, mode); - }); + di->ProcessScene(toscreen); if (mainview) { diff --git a/src/rendering/gl/system/gl_framebuffer.cpp b/src/rendering/gl/system/gl_framebuffer.cpp index 7d99c37517..d85a4b3515 100644 --- a/src/rendering/gl/system/gl_framebuffer.cpp +++ b/src/rendering/gl/system/gl_framebuffer.cpp @@ -519,6 +519,16 @@ FRenderState* OpenGLFrameBuffer::RenderState() return &gl_RenderState; } +void OpenGLFrameBuffer::AmbientOccludeScene(float m5) +{ + gl_RenderState.EnableDrawBuffers(1); + GLRenderer->AmbientOccludeScene(m5); + glViewport(screen->mSceneViewport.left, mSceneViewport.top, mSceneViewport.width, mSceneViewport.height); + GLRenderer->mBuffers->BindSceneFB(true); + gl_RenderState.EnableDrawBuffers(gl_RenderState.GetPassDrawBufferCount()); + gl_RenderState.Apply(); +} + //=========================================================================== // diff --git a/src/rendering/gl/system/gl_framebuffer.h b/src/rendering/gl/system/gl_framebuffer.h index c8663a997a..1875abdbd7 100644 --- a/src/rendering/gl/system/gl_framebuffer.h +++ b/src/rendering/gl/system/gl_framebuffer.h @@ -27,6 +27,7 @@ public: void InitializeState() override; void Update() override; + void AmbientOccludeScene(float m5) override; FRenderState* RenderState() override; void CleanForRestart() override; void UpdatePalette() override; diff --git a/src/rendering/hwrenderer/scene/hw_drawinfo.cpp b/src/rendering/hwrenderer/scene/hw_drawinfo.cpp index 143e779931..15c5ab290a 100644 --- a/src/rendering/hwrenderer/scene/hw_drawinfo.cpp +++ b/src/rendering/hwrenderer/scene/hw_drawinfo.cpp @@ -47,6 +47,7 @@ EXTERN_CVAR(Float, r_visibility) CVAR(Bool, gl_bandedswlight, false, CVAR_ARCHIVE) CVAR(Bool, gl_sort_textures, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +CVAR(Bool, gl_no_skyclear, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) sector_t * hw_FakeFlat(sector_t * sec, sector_t * dest, area_t in_area, bool back); @@ -90,7 +91,6 @@ HWDrawInfo *FDrawInfoList::GetNew() void FDrawInfoList::Release(HWDrawInfo * di) { - di->DrawScene = nullptr; di->ClearBuffers(); di->Level = nullptr; mList.Push(di); @@ -105,7 +105,6 @@ void FDrawInfoList::Release(HWDrawInfo * di) HWDrawInfo *HWDrawInfo::StartDrawInfo(FLevelLocals *lev, HWDrawInfo *parent, FRenderViewpoint &parentvp, HWViewpointUniforms *uniforms) { HWDrawInfo *di = di_list.GetNew(); - if (parent) di->DrawScene = parent->DrawScene; di->Level = lev; di->StartScene(parentvp, uniforms); return di; @@ -642,20 +641,82 @@ void HWDrawInfo::Set3DViewport(FRenderState &state) state.SetStencil(0, SOP_Keep, SF_AllOn); } +//----------------------------------------------------------------------------- +// +// gl_drawscene - this function renders the scene from the current +// viewpoint, including mirrors and skyboxes and other portals +// It is assumed that the HWPortal::EndFrame returns with the +// stencil, z-buffer and the projection matrix intact! +// +//----------------------------------------------------------------------------- + +void HWDrawInfo::DrawScene(int drawmode) +{ + static int recursion = 0; + static int ssao_portals_available = 0; + const auto& vp = Viewpoint; + + bool applySSAO = false; + if (drawmode == DM_MAINVIEW) + { + ssao_portals_available = gl_ssao_portals; + applySSAO = true; + } + else if (drawmode == DM_OFFSCREEN) + { + ssao_portals_available = 0; + } + else if (drawmode == DM_PORTAL && ssao_portals_available > 0) + { + applySSAO = true; + ssao_portals_available--; + } + + if (vp.camera != nullptr) + { + ActorRenderFlags savedflags = vp.camera->renderflags; + CreateScene(drawmode == DM_MAINVIEW); + vp.camera->renderflags = savedflags; + } + else + { + CreateScene(false); + } + auto& RenderState = *screen->RenderState(); + + RenderState.SetDepthMask(true); + if (!gl_no_skyclear) screen->mPortalState->RenderFirstSkyPortal(recursion, this, RenderState); + + RenderScene(RenderState); + + if (applySSAO && RenderState.GetPassType() == GBUFFER_PASS) + { + screen->AmbientOccludeScene(VPUniforms.mProjectionMatrix.get()[5]); + screen->mViewpoints->Bind(RenderState, vpIndex); + } + + // Handle all portals after rendering the opaque objects but before + // doing all translucent stuff + recursion++; + screen->mPortalState->EndFrame(this, RenderState); + recursion--; + RenderTranslucent(RenderState); +} + + //----------------------------------------------------------------------------- // // R_RenderView - renders one view - either the screen or a camera texture // //----------------------------------------------------------------------------- -void HWDrawInfo::ProcessScene(bool toscreen, const std::function &drawScene) +void HWDrawInfo::ProcessScene(bool toscreen) { screen->mPortalState->BeginScene(); int mapsection = Level->PointInRenderSubsector(Viewpoint.Pos)->mapsection; CurrentMapSections.Set(mapsection); - DrawScene = drawScene; - DrawScene(this, toscreen ? DM_MAINVIEW : DM_OFFSCREEN); + DrawScene(toscreen ? DM_MAINVIEW : DM_OFFSCREEN); } diff --git a/src/rendering/hwrenderer/scene/hw_drawinfo.h b/src/rendering/hwrenderer/scene/hw_drawinfo.h index 1a81a4a949..9b86ecf378 100644 --- a/src/rendering/hwrenderer/scene/hw_drawinfo.h +++ b/src/rendering/hwrenderer/scene/hw_drawinfo.h @@ -179,8 +179,6 @@ struct HWDrawInfo fixed_t viewx, viewy; // since the nodes are still fixed point, keeping the view position also fixed point for node traversal is faster. bool multithread; - std::function DrawScene = nullptr; - private: // For ProcessLowerMiniseg bool inview; @@ -246,6 +244,7 @@ public: void SetViewArea(); int SetFullbrightFlags(player_t *player); + void DrawScene(int drawmode); void CreateScene(bool drawpsprites); void RenderScene(FRenderState &state); void RenderTranslucent(FRenderState &state); @@ -253,7 +252,7 @@ public: void EndDrawScene(sector_t * viewsector, FRenderState &state); void DrawEndScene2D(sector_t * viewsector, FRenderState &state); void Set3DViewport(FRenderState &state); - void ProcessScene(bool toscreen, const std::function &drawScene); + void ProcessScene(bool toscreen); bool DoOneSectorUpper(subsector_t * subsec, float planez, area_t in_area); bool DoOneSectorLower(subsector_t * subsec, float planez, area_t in_area); diff --git a/src/rendering/hwrenderer/scene/hw_portal.h b/src/rendering/hwrenderer/scene/hw_portal.h index 88a920a2fa..a6bf05f42f 100644 --- a/src/rendering/hwrenderer/scene/hw_portal.h +++ b/src/rendering/hwrenderer/scene/hw_portal.h @@ -143,7 +143,7 @@ public: { if (Setup(di, state, di->mClipper)) { - di->DrawScene(di, DM_PORTAL); + di->DrawScene(DM_PORTAL); Shutdown(di, state); } else state.ClearScreen(); diff --git a/src/rendering/polyrenderer/backend/poly_framebuffer.cpp b/src/rendering/polyrenderer/backend/poly_framebuffer.cpp index b452e7924c..658b31a804 100644 --- a/src/rendering/polyrenderer/backend/poly_framebuffer.cpp +++ b/src/rendering/polyrenderer/backend/poly_framebuffer.cpp @@ -59,7 +59,6 @@ EXTERN_CVAR(Bool, r_drawvoxels) EXTERN_CVAR(Int, gl_tonemap) EXTERN_CVAR(Int, screenblocks) EXTERN_CVAR(Bool, cl_capfps) -EXTERN_CVAR(Bool, gl_no_skyclear) extern bool NoInterpolateView; extern int rendered_commandbuffers; @@ -340,10 +339,7 @@ sector_t *PolyFrameBuffer::RenderViewpoint(FRenderViewpoint &mainvp, AActor * ca vp.Pos += eye.GetViewShift(vp.HWAngles.Yaw.Degrees); di->SetupView(*GetRenderState(), vp.Pos.X, vp.Pos.Y, vp.Pos.Z, false, false); - // std::function until this can be done better in a cross-API fashion. - di->ProcessScene(toscreen, [&](HWDrawInfo *di, int mode) { - DrawScene(di, mode); - }); + di->ProcessScene(toscreen); if (mainview) { @@ -398,60 +394,6 @@ void PolyFrameBuffer::RenderTextureView(FCanvasTexture *tex, AActor *Viewpoint, tex->SetUpdated(true); } -void PolyFrameBuffer::DrawScene(HWDrawInfo *di, int drawmode) -{ - // To do: this is virtually identical to FGLRenderer::DrawScene and should be merged. - - static int recursion = 0; - static int ssao_portals_available = 0; - const auto &vp = di->Viewpoint; - - bool applySSAO = false; - if (drawmode == DM_MAINVIEW) - { - ssao_portals_available = gl_ssao_portals; - applySSAO = true; - } - else if (drawmode == DM_OFFSCREEN) - { - ssao_portals_available = 0; - } - else if (drawmode == DM_PORTAL && ssao_portals_available > 0) - { - applySSAO = true; - ssao_portals_available--; - } - - if (vp.camera != nullptr) - { - ActorRenderFlags savedflags = vp.camera->renderflags; - di->CreateScene(drawmode == DM_MAINVIEW); - vp.camera->renderflags = savedflags; - } - else - { - di->CreateScene(false); - } - - GetRenderState()->SetDepthMask(true); - if (!gl_no_skyclear) mPortalState->RenderFirstSkyPortal(recursion, di, *GetRenderState()); - - di->RenderScene(*GetRenderState()); - - if (applySSAO && GetRenderState()->GetPassType() == GBUFFER_PASS) - { - //mPostprocess->AmbientOccludeScene(di->VPUniforms.mProjectionMatrix.get()[5]); - //mViewpoints->Bind(*GetRenderState(), di->vpIndex); - } - - // Handle all portals after rendering the opaque objects but before - // doing all translucent stuff - recursion++; - mPortalState->EndFrame(di, *GetRenderState()); - recursion--; - di->RenderTranslucent(*GetRenderState()); -} - static uint8_t ToIntColorComponent(float v) { return clamp((int)(v * 255.0f + 0.5f), 0, 255); @@ -678,3 +620,8 @@ unsigned int PolyFrameBuffer::GetLightBufferBlockSize() const void PolyFrameBuffer::UpdateShadowMap() { } + +void PolyFrameBuffer::AmbientOccludeScene(float m5) +{ + //mPostprocess->AmbientOccludeScene(m5); +} diff --git a/src/rendering/polyrenderer/backend/poly_framebuffer.h b/src/rendering/polyrenderer/backend/poly_framebuffer.h index 776d864127..2b58476237 100644 --- a/src/rendering/polyrenderer/backend/poly_framebuffer.h +++ b/src/rendering/polyrenderer/backend/poly_framebuffer.h @@ -47,6 +47,7 @@ public: void BeginFrame() override; void BlurScene(float amount) override; void PostProcessScene(int fixedcm, const std::function &afterBloomDrawEndScene2D) override; + void AmbientOccludeScene(float m5) override; IHardwareTexture *CreateHardwareTexture() override; FModelRenderer *CreateModelRenderer(int mli) override; @@ -71,7 +72,6 @@ public: private: sector_t *RenderViewpoint(FRenderViewpoint &mainvp, AActor * camera, IntRect * bounds, float fov, float ratio, float fovratio, bool mainview, bool toscreen); void RenderTextureView(FCanvasTexture *tex, AActor *Viewpoint, double FOV); - void DrawScene(HWDrawInfo *di, int drawmode); void UpdateShadowMap(); void CheckCanvas(); diff --git a/src/rendering/v_video.h b/src/rendering/v_video.h index d7d9d7e05f..300b054421 100644 --- a/src/rendering/v_video.h +++ b/src/rendering/v_video.h @@ -311,6 +311,7 @@ public: virtual const char* DeviceName() const { return "Unknown"; } virtual void WriteSavePic(player_t *player, FileWriter *file, int width, int height); virtual sector_t *RenderView(player_t *player) { return nullptr; } + virtual void AmbientOccludeScene(float m5) {} // Screen wiping virtual FTexture *WipeStartScreen(); diff --git a/src/rendering/vulkan/system/vk_framebuffer.cpp b/src/rendering/vulkan/system/vk_framebuffer.cpp index 0c7b9a0e7c..32013a919b 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.cpp +++ b/src/rendering/vulkan/system/vk_framebuffer.cpp @@ -67,7 +67,6 @@ EXTERN_CVAR(Bool, r_drawvoxels) EXTERN_CVAR(Int, gl_tonemap) EXTERN_CVAR(Int, screenblocks) EXTERN_CVAR(Bool, cl_capfps) -EXTERN_CVAR(Bool, gl_no_skyclear) extern bool NoInterpolateView; extern int rendered_commandbuffers; @@ -480,10 +479,7 @@ sector_t *VulkanFrameBuffer::RenderViewpoint(FRenderViewpoint &mainvp, AActor * vp.Pos += eye.GetViewShift(vp.HWAngles.Yaw.Degrees); di->SetupView(*GetRenderState(), vp.Pos.X, vp.Pos.Y, vp.Pos.Z, false, false); - // std::function until this can be done better in a cross-API fashion. - di->ProcessScene(toscreen, [&](HWDrawInfo *di, int mode) { - DrawScene(di, mode); - }); + di->ProcessScene(toscreen); if (mainview) { @@ -548,60 +544,6 @@ void VulkanFrameBuffer::RenderTextureView(FCanvasTexture *tex, AActor *Viewpoint tex->SetUpdated(true); } -void VulkanFrameBuffer::DrawScene(HWDrawInfo *di, int drawmode) -{ - // To do: this is virtually identical to FGLRenderer::DrawScene and should be merged. - - static int recursion = 0; - static int ssao_portals_available = 0; - const auto &vp = di->Viewpoint; - - bool applySSAO = false; - if (drawmode == DM_MAINVIEW) - { - ssao_portals_available = gl_ssao_portals; - applySSAO = true; - } - else if (drawmode == DM_OFFSCREEN) - { - ssao_portals_available = 0; - } - else if (drawmode == DM_PORTAL && ssao_portals_available > 0) - { - applySSAO = true; - ssao_portals_available--; - } - - if (vp.camera != nullptr) - { - ActorRenderFlags savedflags = vp.camera->renderflags; - di->CreateScene(drawmode == DM_MAINVIEW); - vp.camera->renderflags = savedflags; - } - else - { - di->CreateScene(false); - } - - GetRenderState()->SetDepthMask(true); - if (!gl_no_skyclear) mPortalState->RenderFirstSkyPortal(recursion, di, *GetRenderState()); - - di->RenderScene(*GetRenderState()); - - if (applySSAO && GetRenderState()->GetPassType() == GBUFFER_PASS) - { - mPostprocess->AmbientOccludeScene(di->VPUniforms.mProjectionMatrix.get()[5]); - screen->mViewpoints->Bind(*GetRenderState(), di->vpIndex); - } - - // Handle all portals after rendering the opaque objects but before - // doing all translucent stuff - recursion++; - screen->mPortalState->EndFrame(di, *GetRenderState()); - recursion--; - di->RenderTranslucent(*GetRenderState()); -} - void VulkanFrameBuffer::PostProcessScene(int fixedcm, const std::function &afterBloomDrawEndScene2D) { mPostprocess->PostProcessScene(fixedcm, afterBloomDrawEndScene2D); @@ -1000,3 +942,8 @@ FRenderState* VulkanFrameBuffer::RenderState() { return mRenderState.get(); } + +void VulkanFrameBuffer::AmbientOccludeScene(float m5) +{ + mPostprocess->AmbientOccludeScene(m5); +} diff --git a/src/rendering/vulkan/system/vk_framebuffer.h b/src/rendering/vulkan/system/vk_framebuffer.h index 5535f1e8af..e9b6ac1580 100644 --- a/src/rendering/vulkan/system/vk_framebuffer.h +++ b/src/rendering/vulkan/system/vk_framebuffer.h @@ -88,6 +88,7 @@ public: void BeginFrame() override; void BlurScene(float amount) override; void PostProcessScene(int fixedcm, const std::function &afterBloomDrawEndScene2D) override; + void AmbientOccludeScene(float m5) override; IHardwareTexture *CreateHardwareTexture() override; FMaterial* CreateMaterial(FGameTexture* tex, int scaleflags) override; @@ -114,7 +115,6 @@ public: private: sector_t *RenderViewpoint(FRenderViewpoint &mainvp, AActor * camera, IntRect * bounds, float fov, float ratio, float fovratio, bool mainview, bool toscreen); void RenderTextureView(FCanvasTexture *tex, AActor *Viewpoint, double FOV); - void DrawScene(HWDrawInfo *di, int drawmode); void PrintStartupLog(); void CreateFanToTrisIndexBuffer(); void CopyScreenToBuffer(int w, int h, void *data);