From 54327ca859c52fa7f362e60912f0aa0888efc510 Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Sun, 14 Jan 2024 10:32:02 +0800 Subject: [PATCH] Fix the transparent object artifacts on NVIDIA GPUs. --- Plugins/Renderer/gl_hooks.cpp | 4 +- Plugins/Renderer/gl_hud.cpp | 131 +++++++++--------- Plugins/Renderer/gl_hud.h | 19 ++- Plugins/Renderer/gl_light.cpp | 39 ++++-- Plugins/Renderer/gl_light.h | 2 +- Plugins/Renderer/gl_local.h | 12 +- Plugins/Renderer/gl_rmain.cpp | 242 ++++++++++++++++++++------------- Plugins/Renderer/gl_rmisc.cpp | 16 ++- Plugins/Renderer/gl_shadow.cpp | 26 +++- Plugins/Renderer/gl_warp.cpp | 4 + Plugins/Renderer/gl_water.cpp | 5 +- Plugins/Renderer/gl_wsurf.cpp | 7 +- Plugins/Renderer/privatehook.h | 2 +- 13 files changed, 302 insertions(+), 207 deletions(-) diff --git a/Plugins/Renderer/gl_hooks.cpp b/Plugins/Renderer/gl_hooks.cpp index b4078936..b58bbbe4 100644 --- a/Plugins/Renderer/gl_hooks.cpp +++ b/Plugins/Renderer/gl_hooks.cpp @@ -416,7 +416,7 @@ void R_FillAddress(void) } gPrivateFuncs.triapi_RenderMode = gEngfuncs.pTriAPI->RenderMode; - gPrivateFuncs.triapi_Color4f = gEngfuncs.pTriAPI->Color4f; + //gPrivateFuncs.triapi_Color4f = gEngfuncs.pTriAPI->Color4f; bHasOfficialFBOSupport = false; bHasOfficialGLTexAllocSupport = true; @@ -7447,7 +7447,7 @@ hook_t *g_phook_Mod_LoadStudioModel = NULL; hook_t *g_phook_Mod_LoadBrushModel = NULL; hook_t *g_phook_Mod_UnloadSpriteTextures = NULL; hook_t *g_phook_triapi_RenderMode = NULL; -hook_t *g_phook_triapi_Color4f = NULL; +//hook_t *g_phook_triapi_Color4f = NULL; hook_t *g_phook_Draw_MiptexTexture = NULL; hook_t *g_phook_BuildGammaTable = NULL; hook_t *g_phook_DLL_SetModKey = NULL; diff --git a/Plugins/Renderer/gl_hud.cpp b/Plugins/Renderer/gl_hud.cpp index 314e03c3..3e525267 100644 --- a/Plugins/Renderer/gl_hud.cpp +++ b/Plugins/Renderer/gl_hud.cpp @@ -327,6 +327,10 @@ void R_DrawHUDQuad_Texture(int tex, int w, int h) R_DrawHUDQuad(w, h); } +/* + Purpose: Blit src FBO to screen, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ + void GL_BlitFrameFufferToScreen(FBO_Container_t *src) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); @@ -338,6 +342,9 @@ void GL_BlitFrameFufferToScreen(FBO_Container_t *src) glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, glwidth, glheight, GL_COLOR_BUFFER_BIT, GL_LINEAR); } +/* + Purpose: Blit color of src FBO to dst, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ void GL_BlitFrameBufferToFrameBufferColorOnly(FBO_Container_t *src, FBO_Container_t *dst) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->s_hBackBufferFBO); @@ -346,6 +353,9 @@ void GL_BlitFrameBufferToFrameBufferColorOnly(FBO_Container_t *src, FBO_Containe glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, dst->iWidth, dst->iHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR); } +/* + Purpose: Blit color and depth of src FBO to dst, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ void GL_BlitFrameBufferToFrameBufferColorDepth(FBO_Container_t *src, FBO_Container_t *dst) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->s_hBackBufferFBO); @@ -354,6 +364,9 @@ void GL_BlitFrameBufferToFrameBufferColorDepth(FBO_Container_t *src, FBO_Contain glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, dst->iWidth, dst->iHeight, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); } +/* + Purpose: Blit color, depth and stencil of src FBO to dst, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ void GL_BlitFrameBufferToFrameBufferColorDepthStencil(FBO_Container_t* src, FBO_Container_t* dst) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->s_hBackBufferFBO); @@ -362,6 +375,9 @@ void GL_BlitFrameBufferToFrameBufferColorDepthStencil(FBO_Container_t* src, FBO_ glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, dst->iWidth, dst->iHeight, GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST); } +/* + Purpose: Blit depth of src FBO to dst, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ void GL_BlitFrameBufferToFrameBufferDepthOnly(FBO_Container_t* src, FBO_Container_t* dst) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->s_hBackBufferFBO); @@ -370,6 +386,9 @@ void GL_BlitFrameBufferToFrameBufferDepthOnly(FBO_Container_t* src, FBO_Containe glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, dst->iWidth, dst->iHeight, GL_DEPTH_BUFFER_BIT, GL_NEAREST); } +/* + Purpose: Blit stencil of src FBO to dst, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ void GL_BlitFrameBufferToFrameBufferStencilOnly(FBO_Container_t* src, FBO_Container_t* dst) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->s_hBackBufferFBO); @@ -378,6 +397,9 @@ void GL_BlitFrameBufferToFrameBufferStencilOnly(FBO_Container_t* src, FBO_Contai glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, dst->iWidth, dst->iHeight, GL_STENCIL_BUFFER_BIT, GL_NEAREST); } +/* + Purpose: Blit depth and stencil of src FBO to dst, the current rendering FBO will be undefined, you must bind correct FBO again after this +*/ void GL_BlitFrameBufferToFrameBufferDepthStencil(FBO_Container_t* src, FBO_Container_t* dst) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->s_hBackBufferFBO); @@ -386,9 +408,12 @@ void GL_BlitFrameBufferToFrameBufferDepthStencil(FBO_Container_t* src, FBO_Conta glBlitFramebuffer(0, 0, src->iWidth, src->iHeight, 0, 0, dst->iWidth, dst->iHeight, GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, GL_NEAREST); } +/* + Purpose: Do downsample from src FBO to dst, the current rendering FBO is set to dst +*/ void R_DownSample(FBO_Container_t *src, FBO_Container_t *dst, qboolean filter2x2) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -412,7 +437,7 @@ void R_DownSample(FBO_Container_t *src, FBO_Container_t *dst, qboolean filter2x2 void R_LuminPass(FBO_Container_t *src, FBO_Container_t *dst, int type) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -442,7 +467,7 @@ void R_LuminPass(FBO_Container_t *src, FBO_Container_t *dst, int type) void R_LuminAdaptation(FBO_Container_t *src, FBO_Container_t *dst, FBO_Container_t *ada, double frametime) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -465,7 +490,7 @@ void R_LuminAdaptation(FBO_Container_t *src, FBO_Container_t *dst, FBO_Container void R_BrightPass(FBO_Container_t *src, FBO_Container_t *dst, FBO_Container_t *lum) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -489,7 +514,7 @@ void R_BrightPass(FBO_Container_t *src, FBO_Container_t *dst, FBO_Container_t *l void R_BlurPass(FBO_Container_t *src, FBO_Container_t *dst, qboolean vertical) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -514,7 +539,7 @@ void R_BlurPass(FBO_Container_t *src, FBO_Container_t *dst, qboolean vertical) void R_BrightAccum(FBO_Container_t *blur1, FBO_Container_t *blur2, FBO_Container_t *blur3, FBO_Container_t *dst) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -540,7 +565,7 @@ void R_BrightAccum(FBO_Container_t *blur1, FBO_Container_t *blur2, FBO_Container void R_ToneMapping(FBO_Container_t *src, FBO_Container_t *dst, FBO_Container_t *blur, FBO_Container_t *lum) { - glBindFramebuffer(GL_FRAMEBUFFER, dst->s_hBackBufferFBO); + GL_BindFrameBuffer(dst); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); @@ -590,7 +615,7 @@ bool R_IsHDREnabled(void) return true; } -void R_HDR(void) +void R_HDR(FBO_Container_t* src, FBO_Container_t* dst) { GL_BeginProfile(&Profile_DoHDR); @@ -601,8 +626,7 @@ void R_HDR(void) glDisable(GL_BLEND); glColor4f(1, 1, 1, 1); - //Downsample backbuffer - R_DownSample(&s_BackBufferFBO, &s_DownSampleFBO[0], false);//(1->1/4) + R_DownSample(src, &s_DownSampleFBO[0], false);//(1->1/4) R_DownSample(&s_DownSampleFBO[0], &s_DownSampleFBO[1], false);//(1/4)->(1/16) //Log Luminance DownSample from .. (RGB16F to R32F) @@ -636,13 +660,13 @@ void R_HDR(void) R_BrightAccum(&s_BlurPassFBO[0][1], &s_BlurPassFBO[1][1], &s_BlurPassFBO[2][1], &s_BrightAccumFBO); //Tone mapping - R_ToneMapping(&s_BackBufferFBO, &s_ToneMapFBO, &s_BrightAccumFBO, &s_Lumin1x1FBO[last_luminance]); + R_ToneMapping(src, &s_ToneMapFBO, &s_BrightAccumFBO, &s_Lumin1x1FBO[last_luminance]); GL_UseProgram(0); GL_EndFullScreenQuad(); - GL_BlitFrameBufferToFrameBufferColorOnly(&s_ToneMapFBO, &s_BackBufferFBO); + GL_BlitFrameBufferToFrameBufferColorOnly(&s_ToneMapFBO, dst); GL_EndProfile(&Profile_DoHDR); } @@ -664,14 +688,12 @@ bool R_IsFXAAEnabled(void) return true; } -void R_FXAA(void) +void R_FXAA(FBO_Container_t* src, FBO_Container_t* dst) { GL_PushDrawState(); GL_PushMatrix(); - GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); - - GL_BindFrameBuffer(&s_BackBufferFBO); + GL_BindFrameBuffer(dst); GL_UseProgram(pp_fxaa.program); glUniform1i(pp_fxaa.tex0, 0); @@ -683,7 +705,7 @@ void R_FXAA(void) glEnable(GL_TEXTURE_2D); glDisable(GL_BLEND); - R_DrawHUDQuad_Texture(s_BackBufferFBO2.s_hBackBufferTex, glwidth, glheight); + R_DrawHUDQuad_Texture(src->s_hBackBufferTex, glwidth, glheight); GL_UseProgram(0); @@ -717,14 +739,12 @@ bool R_IsUnderWaterEffectEnabled(void) return true; } -void R_UnderWaterEffect(void) +void R_UnderWaterEffect(FBO_Container_t* src, FBO_Container_t* dst) { GL_PushDrawState(); GL_PushMatrix(); - GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); - - GL_BindFrameBuffer(&s_BackBufferFBO); + GL_BindFrameBuffer(dst); GL_UseProgram(under_water_effect.program); glUniform1f(0, r_under_water_effect_wave_amount->value); @@ -736,7 +756,7 @@ void R_UnderWaterEffect(void) glEnable(GL_TEXTURE_2D); glDisable(GL_BLEND); - R_DrawHUDQuad_Texture(s_BackBufferFBO2.s_hBackBufferTex, glwidth, glheight); + R_DrawHUDQuad_Texture(src->s_hBackBufferTex, glwidth, glheight); GL_UseProgram(0); @@ -768,13 +788,11 @@ bool R_IsGammaBlendEnabled() return false; } -void R_GammaCorrection(void) +void R_GammaCorrection(FBO_Container_t* src, FBO_Container_t* dst) { GL_BeginProfile(&Profile_GammaCorrection); - GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); - - GL_BindFrameBuffer(&s_BackBufferFBO); + GL_BindFrameBuffer(dst); GL_BeginFullScreenQuad(false); @@ -787,7 +805,7 @@ void R_GammaCorrection(void) glEnable(GL_TEXTURE_2D); glDisable(GL_BLEND); - R_DrawHUDQuad_Texture(s_BackBufferFBO2.s_hBackBufferTex, r_refdef.vrect->width, r_refdef.vrect->height); + R_DrawHUDQuad_Texture(src->s_hBackBufferTex, r_refdef.vrect->width, r_refdef.vrect->height); GL_UseProgram(0); @@ -796,30 +814,7 @@ void R_GammaCorrection(void) GL_EndProfile(&Profile_GammaCorrection); } -void R_GammaUncorrection(void) -{ - GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); - - GL_BindFrameBuffer(&s_BackBufferFBO); - - GL_BeginFullScreenQuad(false); - - GL_UseProgram(gamma_uncorrection.program); - - //Could be non-fullscreen quad - - GL_Begin2D(); - GL_DisableMultitexture(); - glDisable(GL_BLEND); - - R_DrawHUDQuad_Texture(s_BackBufferFBO2.s_hBackBufferTex, r_refdef.vrect->width, r_refdef.vrect->height); - - GL_UseProgram(0); - - GL_EndFullScreenQuad(); -} - -void R_GammaUncorrectionEx(FBO_Container_t *src, FBO_Container_t* dst) +void R_GammaUncorrection(FBO_Container_t *src, FBO_Container_t* dst) { GL_BindFrameBuffer(dst); @@ -856,11 +851,9 @@ void R_ClearOITBuffer(void) glClearNamedBufferData(r_wsurf.hOITAtomicSSBO, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT, (const void*)&val); } -void R_BlendOITBuffer(void) +void R_BlendOITBuffer(FBO_Container_t* src, FBO_Container_t* dst) { - GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); - - GL_BindFrameBuffer(&s_BackBufferFBO); + GL_BindFrameBuffer(dst); GL_BeginFullScreenQuad(false); @@ -868,7 +861,7 @@ void R_BlendOITBuffer(void) GL_UseProgram(blit_oitblend.program); - GL_Bind(s_BackBufferFBO2.s_hBackBufferTex); + GL_Bind(src->s_hBackBufferTex); glDrawArrays(GL_TRIANGLES, 0, 3); @@ -877,9 +870,9 @@ void R_BlendOITBuffer(void) GL_EndFullScreenQuad(); } -void R_LinearizeDepth(FBO_Container_t *fbo) +void R_LinearizeDepth(FBO_Container_t *src, FBO_Container_t* dst) { - GL_BindFrameBuffer(&s_DepthLinearFBO); + GL_BindFrameBuffer(dst); glDrawBuffer(GL_COLOR_ATTACHMENT0); glDisable(GL_BLEND); @@ -888,7 +881,7 @@ void R_LinearizeDepth(FBO_Container_t *fbo) glUniform4f(0, r_znear * r_zfar, r_znear - r_zfar, r_zfar, r_ortho ? 0 : 1); - GL_Bind(fbo->s_hBackBufferDepthTex); + GL_Bind(src->s_hBackBufferDepthTex); glDrawArrays(GL_TRIANGLES, 0, 3); } @@ -919,7 +912,7 @@ bool R_IsAmbientOcclusionEnabled(void) return true; } -void R_AmbientOcclusion(void) +void R_AmbientOcclusion(FBO_Container_t* src, FBO_Container_t* dst) { if (!hbao_randomview[0]) return; @@ -997,7 +990,7 @@ void R_AmbientOcclusion(void) } //Texture unit 0 = linearized depth - GL_Bind(s_DepthLinearFBO.s_hBackBufferTex); + GL_Bind(src->s_hBackBufferTex); //Texture unit 1 = random texture GL_EnableMultitexture(); @@ -1023,17 +1016,15 @@ void R_AmbientOcclusion(void) glDrawArrays(GL_TRIANGLES, 0, 3); //Write to main framebuffer or GBuffer lightmap channel - if (r_draw_gbuffer) - { - GL_BindFrameBuffer(&s_GBufferFBO); + GL_BindFrameBuffer(dst); + //TODO: wrapper? + if (dst == &s_GBufferFBO) + { glDrawBuffer(GL_COLOR_ATTACHMENT1); } else { - GL_BindFrameBuffer(&s_BackBufferFBO); - - //Should we reset drawbuffer? glDrawBuffer(GL_COLOR_ATTACHMENT0); } @@ -1063,14 +1054,14 @@ void R_AmbientOcclusion(void) GL_EndProfile(&Profile_AmbientOcclusion); } -void R_BlendFinalBuffer(void) +void R_BlendFinalBuffer(FBO_Container_t* src, FBO_Container_t* dst) { GL_BeginProfile(&Profile_BlendFinalBuffer); GL_PushDrawState(); GL_PushMatrix(); - GL_BindFrameBuffer(&s_FinalBufferFBO); + GL_BindFrameBuffer(dst); GL_UseProgram(0); @@ -1080,7 +1071,7 @@ void R_BlendFinalBuffer(void) glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - R_DrawHUDQuad_Texture(s_BackBufferFBO.s_hBackBufferTex, glwidth, glheight); + R_DrawHUDQuad_Texture(src->s_hBackBufferTex, glwidth, glheight); GL_UseProgram(0); diff --git a/Plugins/Renderer/gl_hud.h b/Plugins/Renderer/gl_hud.h index c301785f..bdc1550e 100644 --- a/Plugins/Renderer/gl_hud.h +++ b/Plugins/Renderer/gl_hud.h @@ -161,21 +161,20 @@ extern SHADER_DEFINE(depth_clear); extern SHADER_DEFINE(oitbuffer_clear); extern SHADER_DEFINE(blit_oitblend); -void R_BlendFinalBuffer(void); -void R_BlendOITBuffer(void); +void R_BlendFinalBuffer(FBO_Container_t* src, FBO_Container_t* dst); +void R_BlendOITBuffer(FBO_Container_t* src, FBO_Container_t* dst); void R_ClearOITBuffer(void); -void R_LinearizeDepth(FBO_Container_t *src); -void R_AmbientOcclusion(void); +void R_LinearizeDepth(FBO_Container_t *src, FBO_Container_t* dst); +void R_AmbientOcclusion(FBO_Container_t* src, FBO_Container_t* dst); bool R_IsGammaBlendEnabled(); -void R_GammaCorrection(void); -void R_GammaUncorrection(void); -void R_GammaUncorrectionEx(FBO_Container_t* src, FBO_Container_t* dst); +void R_GammaCorrection(FBO_Container_t* src, FBO_Container_t* dst); +void R_GammaUncorrection(FBO_Container_t* src, FBO_Container_t* dst); bool R_IsAmbientOcclusionEnabled(void); -void R_HDR(void); +void R_HDR(FBO_Container_t* src, FBO_Container_t*); bool R_IsHDREnabled(void); -void R_FXAA(void); +void R_FXAA(FBO_Container_t* src, FBO_Container_t* dst); bool R_IsFXAAEnabled(void); -void R_UnderWaterEffect(void); +void R_UnderWaterEffect(FBO_Container_t* src, FBO_Container_t* dst); bool R_IsUnderWaterEffectEnabled(void); void GL_BlitFrameFufferToScreen(FBO_Container_t *src); void GL_BlitFrameBufferToFrameBufferColorOnly(FBO_Container_t *src, FBO_Container_t *dst); diff --git a/Plugins/Renderer/gl_light.cpp b/Plugins/Renderer/gl_light.cpp index 59f01599..cce1053d 100644 --- a/Plugins/Renderer/gl_light.cpp +++ b/Plugins/Renderer/gl_light.cpp @@ -527,6 +527,11 @@ bool R_IsDeferredRenderingEnabled(void) return true; } +/* + + Purpose : Switch to s_GBuffers +*/ + bool R_BeginRenderGBuffer(void) { if (!R_IsDeferredRenderingEnabled()) @@ -979,14 +984,17 @@ void R_LightShadingPass(void) } -//Final Shading Pass - -void R_FinalShadingPass(void) +/* + Purpose : final shading colors from s_GBufferFBO to dst, simply blits depth and stencil. +*/ +void R_FinalShadingPass(FBO_Container_t *dst) { //Re-enable depth write glDepthMask(GL_TRUE); //Write GBuffer depth and stencil buffer into main framebuffer + GL_BlitFrameBufferToFrameBufferDepthStencil(&s_GBufferFBO, dst); +#if 0 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, s_BackBufferFBO.s_hBackBufferFBO); glBindFramebuffer(GL_READ_FRAMEBUFFER, s_GBufferFBO.s_hBackBufferFBO); glBlitFramebuffer(0, 0, s_GBufferFBO.iWidth, s_GBufferFBO.iHeight, @@ -995,13 +1003,16 @@ void R_FinalShadingPass(void) GL_NEAREST); GL_BindFrameBuffer(&s_BackBufferFBO); +#endif - //No blend for final shading pass - glDisable(GL_BLEND); + GL_BindFrameBuffer(dst); //Only draw color0 channel glDrawBuffer(GL_COLOR_ATTACHMENT0); + //No blend for final shading pass + glDisable(GL_BLEND); + GL_BeginFullScreenQuad(false); program_state_t FinalProgramState = 0; @@ -1064,30 +1075,28 @@ void R_FinalShadingPass(void) GL_EndFullScreenQuad(); } -void R_EndRenderGBuffer(void) +void R_EndRenderGBuffer(FBO_Container_t *dst) { GL_BeginFullScreenQuad(false); - R_LinearizeDepth(&s_GBufferFBO); + R_LinearizeDepth(&s_GBufferFBO, &s_DepthLinearFBO); if (R_IsAmbientOcclusionEnabled()) { - R_AmbientOcclusion(); - } - else - { - //Write to GBuffer->lightmap only - GL_BindFrameBuffer(&s_GBufferFBO); - glDrawBuffer(GL_COLOR_ATTACHMENT1); + R_AmbientOcclusion(&s_DepthLinearFBO, &s_GBufferFBO); } + //Write to GBuffer->lightmap only whatsoever + GL_BindFrameBuffer(&s_GBufferFBO); + glDrawBuffer(GL_COLOR_ATTACHMENT1); + GL_EndFullScreenQuad(); GL_BeginProfile(&Profile_EndRenderGBuffer); R_LightShadingPass(); - R_FinalShadingPass(); + R_FinalShadingPass(dst); GL_EndProfile(&Profile_EndRenderGBuffer); diff --git a/Plugins/Renderer/gl_light.h b/Plugins/Renderer/gl_light.h index 56b9823f..57d3b202 100644 --- a/Plugins/Renderer/gl_light.h +++ b/Plugins/Renderer/gl_light.h @@ -84,7 +84,7 @@ void R_NewMapLight(void); void R_InitLight(void); void R_ShutdownLight(void); bool R_BeginRenderGBuffer(void); -void R_EndRenderGBuffer(void); +void R_EndRenderGBuffer(FBO_Container_t* dst); void R_SetGBufferMask(int mask); void R_SetGBufferBlend(int blendsrc, int blenddst); void R_SaveDLightProgramStates(void); diff --git a/Plugins/Renderer/gl_local.h b/Plugins/Renderer/gl_local.h index 1271bd13..7faa8a7b 100644 --- a/Plugins/Renderer/gl_local.h +++ b/Plugins/Renderer/gl_local.h @@ -206,6 +206,7 @@ extern bool bHasOfficialGLTexAllocSupport; extern FBO_Container_t s_FinalBufferFBO; extern FBO_Container_t s_BackBufferFBO; extern FBO_Container_t s_BackBufferFBO2; +extern FBO_Container_t s_BackBufferFBO3; extern FBO_Container_t s_GBufferFBO; extern FBO_Container_t s_BlendBufferFBO; extern FBO_Container_t s_DownSampleFBO[DOWNSAMPLE_BUFFERS]; @@ -219,7 +220,8 @@ extern FBO_Container_t s_DepthLinearFBO; extern FBO_Container_t s_HBAOCalcFBO; extern FBO_Container_t s_ShadowFBO; -extern FBO_Container_t *g_CurrentFBO; +extern FBO_Container_t* g_CurrentSceneFBO; +extern FBO_Container_t *g_CurrentRenderingFBO; extern msurface_t **skychain; extern msurface_t **waterchain; @@ -384,7 +386,7 @@ void GL_SelectTexture(GLenum target); void GL_DisableMultitexture(void); void GL_EnableMultitexture(void); void triapi_RenderMode(int mode); -void triapi_Color4f(float x, float y, float z, float w); +//void triapi_Color4f(float x, float y, float z, float w); void GL_UnloadTextureByIdentifier(const char* identifier, bool notify_callback); void GL_UnloadTextures(void); int GL_LoadTexture(char *identifier, GL_TEXTURETYPE textureType, int width, int height, byte *data, qboolean mipmap, int iType, byte *pPal); @@ -434,7 +436,10 @@ void GL_UploadTextureArrayColorFormat(int texid, int w, int h, int levels, int i GLuint GL_GenShadowTexture(int w, int h, float *borderColor); void GL_UploadShadowTexture(int texid, int w, int h, float *borderColor); -FBO_Container_t* GL_GetCurrentFrameBuffer(); +void GL_SetCurrentSceneFBO(FBO_Container_t* src); +FBO_Container_t* GL_GetCurrentSceneFBO(); +FBO_Container_t* GL_GetCurrentRenderingFBO(); + void GL_BindFrameBuffer(FBO_Container_t *fbo); void GL_BindFrameBufferWithTextures(FBO_Container_t *fbo, GLuint color, GLuint depth, GLuint depth_stencil, GLsizei width, GLsizei height); @@ -471,7 +476,6 @@ void GL_PushFrameBuffer(void); void GL_PopFrameBuffer(void); bool R_IsRenderingGBuffer(); -bool R_IsRenderingBackBuffer(); //refdef void R_PushRefDef(void); diff --git a/Plugins/Renderer/gl_rmain.cpp b/Plugins/Renderer/gl_rmain.cpp index 02fd28ba..3636837d 100644 --- a/Plugins/Renderer/gl_rmain.cpp +++ b/Plugins/Renderer/gl_rmain.cpp @@ -193,6 +193,7 @@ int glheight = 0; FBO_Container_t s_FinalBufferFBO = { 0 }; FBO_Container_t s_BackBufferFBO = { 0 }; FBO_Container_t s_BackBufferFBO2 = { 0 }; +FBO_Container_t s_BackBufferFBO3 = { 0 }; FBO_Container_t s_GBufferFBO = { 0 }; FBO_Container_t s_DownSampleFBO[DOWNSAMPLE_BUFFERS] = { 0 }; FBO_Container_t s_LuminFBO[LUMIN_BUFFERS] = { 0 }; @@ -205,7 +206,8 @@ FBO_Container_t s_DepthLinearFBO = { 0 }; FBO_Container_t s_HBAOCalcFBO = { 0 }; FBO_Container_t s_ShadowFBO = { 0 }; -FBO_Container_t *g_CurrentFBO = NULL; +FBO_Container_t* g_CurrentSceneFBO = NULL; +FBO_Container_t *g_CurrentRenderingFBO = NULL; bool bEnforceStretchAspect = false; bool bUseBindless = true; @@ -303,12 +305,7 @@ cvar_t *r_additive_shift = NULL; bool R_IsRenderingGBuffer() { - return GL_GetCurrentFrameBuffer() == &s_GBufferFBO; -} - -bool R_IsRenderingBackBuffer() -{ - return GL_GetCurrentFrameBuffer() == &s_BackBufferFBO; + return GL_GetCurrentRenderingFBO() == &s_GBufferFBO; } qboolean Host_IsSinglePlayerGame() @@ -471,7 +468,6 @@ const color24 gTracerColors[] = void R_DrawParticles(void) { - //Don't render particles!!! if (CL_IsDevOverviewMode()) return; @@ -522,7 +518,11 @@ void R_DrawParticles(void) R_UseLegacySpriteProgram(LegacySpriteProgramState, NULL); - glBegin(GL_TRIANGLES); +#if 1 + gEngfuncs.pTriAPI->Begin(TRI_TRIANGLES); +#else + //glBegin(GL_TRIANGLES); +#endif VectorScale(vup, 1.5, up); VectorScale(vright, 1.5, right); @@ -538,7 +538,7 @@ void R_DrawParticles(void) { if (p->type != pt_blob) { - // hack a scale up to keep particles from disapearing + // hack a scale up to keep particles from disappearing scale = (p->org[0] - r_origin[0])*vpn[0] + (p->org[1] - r_origin[1])*vpn[1] + (p->org[2] - r_origin[2])*vpn[2]; if (scale < 20) @@ -554,6 +554,16 @@ void R_DrawParticles(void) rgba[2] = pb[0]; rgba[3] = 255; +#if 1 + + gEngfuncs.pTriAPI->Color4ub(rgba[0], rgba[1], rgba[2], rgba[3]); + gEngfuncs.pTriAPI->TexCoord2f(0, 0); + gEngfuncs.pTriAPI->Vertex3fv(p->org); + gEngfuncs.pTriAPI->TexCoord2f(1, 0); + gEngfuncs.pTriAPI->Vertex3f(p->org[0] + up[0] * scale, p->org[1] + up[1] * scale, p->org[2] + up[2] * scale); + gEngfuncs.pTriAPI->TexCoord2f(0, 1); + gEngfuncs.pTriAPI->Vertex3f(p->org[0] + right[0] * scale, p->org[1] + right[1] * scale, p->org[2] + right[2] * scale); +#else glColor3ubv(rgba); glTexCoord2f(0, 0); glVertex3fv(p->org); @@ -561,6 +571,7 @@ void R_DrawParticles(void) glVertex3f(p->org[0] + up[0] * scale, p->org[1] + up[1] * scale, p->org[2] + up[2] * scale); glTexCoord2f(0, 1); glVertex3f(p->org[0] + right[0] * scale, p->org[1] + right[1] * scale, p->org[2] + right[2] * scale); +#endif } if (p->type != pt_clientcustom) @@ -655,7 +666,11 @@ void R_DrawParticles(void) } } - glEnd(); +#if 1 + gEngfuncs.pTriAPI->End(); +#else + //glEnd(); +#endif gPrivateFuncs.R_TracerDraw(); gPrivateFuncs.R_BeamDrawList(); @@ -664,10 +679,12 @@ void R_DrawParticles(void) glDisable(GL_ALPHA_TEST); } +#if 0 void triapi_Color4f(float x, float y, float z, float w) { gPrivateFuncs.triapi_Color4f(x, y, z, w); } +#endif void triapi_RenderMode(int mode) { @@ -887,7 +904,8 @@ void R_DrawTransEntities(int onlyClientDraw) glColorMask(1, 1, 1, 1); - R_BlendOITBuffer(); + GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); + R_BlendOITBuffer(&s_BackBufferFBO2, &s_BackBufferFBO); glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); } @@ -1353,20 +1371,20 @@ void R_SetupPerspective(float fovx, float fovy, float zNear, float zFar) VectorNormalize(r_frustum_vec[3]); } -void GL_GenerateFrameBuffers(void) +void GL_FreeFrameBuffers(void) { GL_FreeFBO(&s_FinalBufferFBO); GL_FreeFBO(&s_BackBufferFBO); GL_FreeFBO(&s_BackBufferFBO2); GL_FreeFBO(&s_GBufferFBO); - for(int i = 0; i < DOWNSAMPLE_BUFFERS; ++i) + for (int i = 0; i < DOWNSAMPLE_BUFFERS; ++i) GL_FreeFBO(&s_DownSampleFBO[i]); - for(int i = 0; i < LUMIN_BUFFERS; ++i) + for (int i = 0; i < LUMIN_BUFFERS; ++i) GL_FreeFBO(&s_LuminFBO[i]); - for(int i = 0; i < LUMIN1x1_BUFFERS; ++i) + for (int i = 0; i < LUMIN1x1_BUFFERS; ++i) GL_FreeFBO(&s_Lumin1x1FBO[i]); GL_FreeFBO(&s_BrightPassFBO); - for(int i = 0; i < BLUR_BUFFERS; ++i) + for (int i = 0; i < BLUR_BUFFERS; ++i) { GL_FreeFBO(&s_BlurPassFBO[i][0]); GL_FreeFBO(&s_BlurPassFBO[i][1]); @@ -1376,9 +1394,11 @@ void GL_GenerateFrameBuffers(void) GL_FreeFBO(&s_DepthLinearFBO); GL_FreeFBO(&s_HBAOCalcFBO); GL_FreeFBO(&s_ShadowFBO); - - //Probably useless - //glEnable(GL_TEXTURE_2D); +} + +void GL_GenerateFrameBuffers(void) +{ + GL_FreeFrameBuffers(); s_FinalBufferFBO.iWidth = glwidth; s_FinalBufferFBO.iHeight = glheight; @@ -1416,6 +1436,18 @@ void GL_GenerateFrameBuffers(void) g_pMetaHookAPI->SysError("Failed to initialize BackBufferFBO2!\n"); } + s_BackBufferFBO3.iWidth = glwidth; + s_BackBufferFBO3.iHeight = glheight; + GL_GenFrameBuffer(&s_BackBufferFBO3); + GL_FrameBufferColorTexture(&s_BackBufferFBO3, GL_RGBA16F); + GL_FrameBufferDepthTexture(&s_BackBufferFBO3, GL_DEPTH24_STENCIL8); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + { + GL_FreeFBO(&s_BackBufferFBO3); + g_pMetaHookAPI->SysError("Failed to initialize BackBufferFBO3!\n"); + } + s_GBufferFBO.iWidth = glwidth; s_GBufferFBO.iHeight = glheight; GL_GenFrameBuffer(&s_GBufferFBO); @@ -1674,32 +1706,17 @@ void GL_Shutdown(void) { GL_FreeShaders(); GL_FreeProfiles(); - - GL_FreeFBO(&s_FinalBufferFBO); - GL_FreeFBO(&s_BackBufferFBO); - GL_FreeFBO(&s_BackBufferFBO2); - GL_FreeFBO(&s_GBufferFBO); - for (int i = 0; i < DOWNSAMPLE_BUFFERS; ++i) - GL_FreeFBO(&s_DownSampleFBO[i]); - for (int i = 0; i < LUMIN_BUFFERS; ++i) - GL_FreeFBO(&s_LuminFBO[i]); - for (int i = 0; i < LUMIN1x1_BUFFERS; ++i) - GL_FreeFBO(&s_Lumin1x1FBO[i]); - for (int i = 0; i < BLUR_BUFFERS; ++i) - { - GL_FreeFBO(&s_BlurPassFBO[i][0]); - GL_FreeFBO(&s_BlurPassFBO[i][1]); - } - GL_FreeFBO(&s_ToneMapFBO); - GL_FreeFBO(&s_DepthLinearFBO); - GL_FreeFBO(&s_HBAOCalcFBO); - GL_FreeFBO(&s_ShadowFBO); + GL_FreeFrameBuffers(); } +/* + Purpose : Switch to s_FinalBufferFBO and clear it +*/ + void GL_FlushFinalBuffer() { GL_BindFrameBuffer(&s_FinalBufferFBO); - + glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } @@ -1723,7 +1740,7 @@ void R_RenderStartView() } /* - Called only once per frame, before running any render pass + Purpose: Called once per frame, before running any render pass, but after physics and networking */ void R_RenderStartFrame() @@ -1760,7 +1777,7 @@ void GL_BeginRendering(int *x, int *y, int *width, int *height) glwidth = (*width); glheight = (*height); - //No V_RenderView calls when level changes so don't clear final buffer + //No V_RenderView calls when level changes so don't GL_FlushFinalBuffer, this replicates vanilla engine's behavior if (SCR_IsLoadingVisible()) { @@ -1777,8 +1794,16 @@ void GL_BeginRendering(int *x, int *y, int *width, int *height) *c_brush_polys = 0; } +/* + Purpose: + Called on beginning of RenderView + This will switch from final framebuffer (RGBA8) to back framebuffer (RGBAF16) +*/ + void R_PreRenderView() { + //Reset statistics + r_wsurf_drawcall = 0; r_wsurf_polys = 0; r_studio_drawcall = 0; @@ -1786,10 +1811,8 @@ void R_PreRenderView() r_sprite_drawcall = 0; r_sprite_polys = 0; - //Capture previous fog settings - + //Capture OpenGL fog settings committed by client dll or engine dll. r_fog_mode = 0; - r_draw_gammablend = false; if (glIsEnabled(GL_FOG)) { @@ -1817,14 +1840,36 @@ void R_PreRenderView() } } + //Always force GammaBlend to be disabled when rendering opaques. + r_draw_gammablend = false; + + //Currently unused R_RenderStartView(); R_RenderShadowMap(); R_RenderWaterPass(); - //Restore BackBufferFBO states + //Restore s_BackBufferFBO states because it might be corrupted by R_RenderWaterPass. GL_BindFrameBufferWithTextures(&s_BackBufferFBO, s_BackBufferFBO.s_hBackBufferTex, 0, s_BackBufferFBO.s_hBackBufferDepthTex, glwidth, glheight); + GL_SetCurrentSceneFBO(&s_BackBufferFBO); + + vec4_t vecClearColor = {0}; + + if (CL_IsDevOverviewMode()) + { + R_ParseCvarAsColor3(dev_overview_color, vecClearColor); + } + else + { + R_ParseCvarAsColor3(gl_clearcolor, vecClearColor); + } + + //TODO: Gamma correction for the clear color ? + GL_ClearColorDepthStencil(vecClearColor, 1, STENCIL_MASK_WORLD, STENCIL_MASK_ALL); + + glDepthFunc(GL_LEQUAL); + glDepthRange(0, 1); } void R_PostRenderView() @@ -1833,30 +1878,50 @@ void R_PostRenderView() { if (r_draw_gammablend) { - R_GammaUncorrection(); + R_GammaUncorrection(GL_GetCurrentSceneFBO(), &s_BackBufferFBO2); + } + else + { + GL_BlitFrameBufferToFrameBufferColorOnly(GL_GetCurrentSceneFBO(), &s_BackBufferFBO2); + } + + R_HDR(&s_BackBufferFBO2, &s_BackBufferFBO); + + if (GL_GetCurrentSceneFBO() != &s_BackBufferFBO) + { + GL_BlitFrameBufferToFrameBufferDepthStencil(GL_GetCurrentSceneFBO(), &s_BackBufferFBO); } - R_HDR(); } else { - if (!r_draw_gammablend) + if (r_draw_gammablend) + { + GL_BlitFrameBufferToFrameBufferColorDepthStencil(GL_GetCurrentSceneFBO(), &s_BackBufferFBO); + } + else { - R_GammaCorrection(); + GL_BlitFrameBufferToFrameBufferColorOnly(GL_GetCurrentSceneFBO(), &s_BackBufferFBO2); + R_GammaCorrection(&s_BackBufferFBO2, &s_BackBufferFBO); } } r_draw_gammablend = false; + GL_BindFrameBuffer(&s_BackBufferFBO); + GL_SetCurrentSceneFBO(&s_BackBufferFBO); if (R_IsUnderWaterEffectEnabled()) { - R_UnderWaterEffect(); + GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); + R_UnderWaterEffect(&s_BackBufferFBO2, &s_BackBufferFBO); } if (R_IsFXAAEnabled()) { - R_FXAA(); + GL_BlitFrameBufferToFrameBufferColorOnly(&s_BackBufferFBO, &s_BackBufferFBO2); + R_FXAA(&s_BackBufferFBO2, &s_BackBufferFBO); } + //Restore OpenGL states to default GL_DisableMultitexture(); glEnable(GL_TEXTURE_2D); glColor4f(1, 1, 1, 1); @@ -1941,18 +2006,28 @@ bool R_IsRenderingPortal(void) return g_bRenderingPortals_SCClient && (*g_bRenderingPortals_SCClient) == 1; } +void R_ClearPortalClipPlanes(void) +{ + for (int i = 0; i < 6; ++i) + { + g_bPortalClipPlaneEnabled[i] = false; + } + memset(g_PortalClipPlane, 0, sizeof(g_PortalClipPlane)); +} + void R_RenderView_SvEngine(int viewIdx) { - //Clear texture id cache since SC client dll bind texture id 0 but leave texture id cache non-zero - *currenttexture = -1; + //Clear texture id cache since SC client dll bind texture id 0 by glBindTexture directly and leave texture id caching system corrupted. + (*currenttexture) = -1; - //Clear final buffer again since SC client dll may draw some portal views on it before the first pass. + //Clear s_FinalBuffer again since SC client dll may draw some portal views on it before the very first pass. if (R_IsRenderingPortal()) { GL_FlushFinalBuffer(); } else { + //Clear s_FinalBuffer for the very first pass if (viewIdx == 0) { GL_FlushFinalBuffer(); @@ -1975,27 +2050,8 @@ void R_RenderView_SvEngine(int viewIdx) g_pMetaHookAPI->SysError("R_RenderView: NULL worldmodel"); } - //This will switch from final framebuffer (RGBA8) to back framebuffer (RGBAF16) R_PreRenderView(); - //back framebuffer should be cleared before uses. - float clearColor[3]; - - if (CL_IsDevOverviewMode()) - { - R_ParseCvarAsColor3(dev_overview_color, clearColor); - } - else - { - R_ParseCvarAsColor3(gl_clearcolor, clearColor); - } - - vec4_t vecClearColor = { clearColor[0], clearColor[1], clearColor[2], 0 }; - GL_ClearColorDepthStencil(vecClearColor, 1, STENCIL_MASK_WORLD, STENCIL_MASK_ALL); - - glDepthFunc(GL_LEQUAL); - glDepthRange(0, 1); - if (!(*r_refdef.onlyClientDraws)) { //Allocate TEMPENT here @@ -2011,11 +2067,11 @@ void R_RenderView_SvEngine(int viewIdx) R_DrawViewModel(); } - //Post processing R_PostRenderView(); //This will switch to final framebuffer (RGBA8) - R_BlendFinalBuffer(); + //TODO: Why not using GL_BlitFrameBufferToFrameBufferColorOnly? + R_BlendFinalBuffer(&s_BackBufferFBO, &s_FinalBufferFBO); if (!(*r_refdef.onlyClientDraws)) R_PolyBlend(); @@ -2027,20 +2083,13 @@ void R_RenderView_SvEngine(int viewIdx) GL_BindFrameBuffer(&s_FinalBufferFBO); } - *c_alias_polys += r_studio_polys; - *c_brush_polys += r_wsurf_polys; + (*c_alias_polys) += r_studio_polys; + (*c_brush_polys) += r_wsurf_polys; //Clear texture id cache since SC client dll bind texture id 0 but leave texture id cache non-zero - *currenttexture = -1; + (*currenttexture) = -1; - //Clear portal clipplanes - - for (int i = 0; i < 6; ++i) - { - g_bPortalClipPlaneEnabled[i] = false; - } - - memset(g_PortalClipPlane, 0, sizeof(g_PortalClipPlane)); + R_ClearPortalClipPlanes(); if (r_speeds->value) { @@ -2051,7 +2100,7 @@ void R_RenderView_SvEngine(int viewIdx) auto time2 = gEngfuncs.GetAbsoluteTime(); - gEngfuncs.Con_Printf("%3ifps in %3i ms at pass %d, with %d brushpolys, %d brushdraw, %d studiopolys, %d studiodraw, %d spritepolys, %d spritedraw\n", + gEngfuncs.Con_Printf("%3ifps in %3i ms at viewpass #%d, with:\n %d brushpolys,%d brushdraw.\n %d studiopolys, %d studiodraw.\n %d spritepolys, %d spritedraw.\n", (int)(framerate + 0.5), (int)((time2 - time1) * 1000), r_renderview_pass, r_wsurf_polys, r_wsurf_drawcall, @@ -3372,23 +3421,24 @@ void R_EndRenderOpaque(void) glDisable(GL_ALPHA_TEST); - //Transfer everything from GBuffer into BackBuffer + //Transfer everything from GBuffer into SceneFBO if (R_IsRenderingGBuffer()) { - R_EndRenderGBuffer(); + R_EndRenderGBuffer(GL_GetCurrentSceneFBO()); } else if (R_IsAmbientOcclusionEnabled()) { GL_BeginFullScreenQuad(false); - R_LinearizeDepth(&s_BackBufferFBO); - R_AmbientOcclusion(); + R_LinearizeDepth(GL_GetCurrentSceneFBO(), &s_DepthLinearFBO); + R_AmbientOcclusion(&s_DepthLinearFBO, GL_GetCurrentSceneFBO()); GL_EndFullScreenQuad(); } if (R_IsGammaBlendEnabled()) { - R_GammaCorrection(); - + R_GammaCorrection(GL_GetCurrentSceneFBO(), &s_BackBufferFBO3); + GL_BlitFrameBufferToFrameBufferDepthStencil(GL_GetCurrentSceneFBO(), &s_BackBufferFBO3); + GL_SetCurrentSceneFBO(&s_BackBufferFBO3); r_draw_gammablend = true; } } @@ -3423,7 +3473,7 @@ void ClientDLL_DrawNormalTriangles(void) glAlphaFunc(GL_NOTEQUAL, 0); //Clear texture id cache since SC client dll bind texture id 0 but leave texture id cache non-zero - *currenttexture = -1; + (*currenttexture) = -1; //Restore current framebuffer just in case that Allow SC client dll changes it GL_PopFrameBuffer(); diff --git a/Plugins/Renderer/gl_rmisc.cpp b/Plugins/Renderer/gl_rmisc.cpp index b54be1c2..a2ae1bbf 100644 --- a/Plugins/Renderer/gl_rmisc.cpp +++ b/Plugins/Renderer/gl_rmisc.cpp @@ -25,14 +25,24 @@ GLint save_readframebuffer[MAX_SAVESTACK] = { 0 }; GLint save_drawframebuffer[MAX_SAVESTACK] = { 0 }; int save_framebuffer_stack = 0; -FBO_Container_t* GL_GetCurrentFrameBuffer() +void GL_SetCurrentSceneFBO(FBO_Container_t* src) { - return g_CurrentFBO; + g_CurrentSceneFBO = src; +} + +FBO_Container_t* GL_GetCurrentSceneFBO() +{ + return g_CurrentSceneFBO; +} + +FBO_Container_t* GL_GetCurrentRenderingFBO() +{ + return g_CurrentRenderingFBO; } void GL_BindFrameBuffer(FBO_Container_t *fbo) { - g_CurrentFBO = fbo; + g_CurrentRenderingFBO = fbo; if (fbo) { diff --git a/Plugins/Renderer/gl_shadow.cpp b/Plugins/Renderer/gl_shadow.cpp index c134532f..81f72e13 100644 --- a/Plugins/Renderer/gl_shadow.cpp +++ b/Plugins/Renderer/gl_shadow.cpp @@ -301,6 +301,11 @@ bool R_ShouldCastShadow(cl_entity_t *ent) return false; } +/* + + Purpose : Rendering textures for shadow mapping + +*/ void R_RenderShadowScene(void) { GL_BeginProfile(&Profile_RenderShadowScene); @@ -351,7 +356,6 @@ void R_RenderShadowScene(void) r_draw_shadowscene = true; GL_BindFrameBuffer(&s_ShadowFBO); - glDrawBuffer(GL_COLOR_ATTACHMENT0); glDisable(GL_BLEND); glDisable(GL_ALPHA_TEST); @@ -367,6 +371,7 @@ void R_RenderShadowScene(void) glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, r_shadow_texture.color_array_as_depth, 0, i); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, r_shadow_texture.depth_stencil, 0); + glDrawBuffer(GL_COLOR_ATTACHMENT0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -546,7 +551,13 @@ void R_RenderShadowDynamicLights(void) } } -void R_RenderShadowMap(void) +/* + + Purpose : Clear shadow related vars which might be accessed later by deferred lighting pass. + +*/ + +void R_RenderShadowMap_Start(void) { shadow_numvisedicts[0] = 0; shadow_numvisedicts[1] = 0; @@ -556,6 +567,17 @@ void R_RenderShadowMap(void) { cl_dlight_shadow_textures[i].ready = false; } +} + +/* + + Purpose : Rendering textures for shadow mapping + +*/ + +void R_RenderShadowMap(void) +{ + R_RenderShadowMap_Start(); if (!r_shadow->value) return; diff --git a/Plugins/Renderer/gl_warp.cpp b/Plugins/Renderer/gl_warp.cpp index a88a2995..4674e322 100644 --- a/Plugins/Renderer/gl_warp.cpp +++ b/Plugins/Renderer/gl_warp.cpp @@ -7,6 +7,10 @@ int *r_loading_skybox = NULL; cvar_t* r_detailskytextures = NULL; +/* + Purpose : Draw skybox +*/ + void R_DrawSkyBox(void) { if (CL_IsDevOverviewMode()) diff --git a/Plugins/Renderer/gl_water.cpp b/Plugins/Renderer/gl_water.cpp index bacea626..7a481135 100644 --- a/Plugins/Renderer/gl_water.cpp +++ b/Plugins/Renderer/gl_water.cpp @@ -696,6 +696,7 @@ void R_RenderReflectView(water_reflect_cache_t *ReflectCache) g_CurrentReflectCache = ReflectCache; GL_BindFrameBufferWithTextures(&s_BackBufferFBO, ReflectCache->reflectmap, 0, ReflectCache->depthreflmap, ReflectCache->texwidth, ReflectCache->texheight); + GL_SetCurrentSceneFBO(&s_BackBufferFBO); vec4_t vecClearColor = { ReflectCache->color.r / 255.0f, ReflectCache->color.g / 255.0f, ReflectCache->color.b / 255.0f, 0 }; GL_ClearColorDepthStencil(vecClearColor, 1, STENCIL_MASK_SKY, STENCIL_MASK_ALL); @@ -750,6 +751,8 @@ void R_RenderReflectView(water_reflect_cache_t *ReflectCache) r_draw_reflectview = false; g_CurrentReflectCache = NULL; + + GL_SetCurrentSceneFBO(NULL); } void R_RenderWaterPass(void) @@ -996,7 +999,7 @@ void R_DrawWaterVBO(water_vbo_t *WaterVBO, water_reflect_cache_t *ReflectCache, //s_BackBufferFBO is in gamma space GL_BlitFrameBufferToFrameBufferDepthStencil(&s_BackBufferFBO, &s_BackBufferFBO2); //Convert back to linear space - R_GammaUncorrectionEx(&s_BackBufferFBO, &s_BackBufferFBO2); + R_GammaUncorrection(&s_BackBufferFBO, &s_BackBufferFBO2); } else { diff --git a/Plugins/Renderer/gl_wsurf.cpp b/Plugins/Renderer/gl_wsurf.cpp index 2de631fa..e8f7613c 100644 --- a/Plugins/Renderer/gl_wsurf.cpp +++ b/Plugins/Renderer/gl_wsurf.cpp @@ -4294,6 +4294,10 @@ void R_SetupDLightUBO(void) } } +/* + Purpose : Setup texture states and SceneUBO for DrawWorld +*/ + void R_PrepareDrawWorld(void) { r_wsurf.bDiffuseTexture = true; @@ -4328,8 +4332,6 @@ void R_PrepareDrawWorld(void) glMatrixMode(GL_MODELVIEW); } - //Setup Scene UBO - R_SetupSceneUBO(); R_SetupDLightUBO(); } @@ -4372,6 +4374,7 @@ void R_DrawWorld(void) R_DrawSkyBox(); + //Skip world meshes if we are drawing reflect texture for skybox. if (r_draw_reflectview && g_CurrentReflectCache->level == WATER_LEVEL_REFLECT_SKYBOX) { diff --git a/Plugins/Renderer/privatehook.h b/Plugins/Renderer/privatehook.h index 92a1264c..f09e250c 100644 --- a/Plugins/Renderer/privatehook.h +++ b/Plugins/Renderer/privatehook.h @@ -79,7 +79,7 @@ typedef struct void(*Mod_LoadBrushModel)(model_t *mod, void *buffer); model_t *(*Mod_LoadModel)(model_t *mod, qboolean crash, qboolean trackCRC); void(*triapi_RenderMode)(int mode); - void(*triapi_Color4f) (float r, float g, float b, float a); + //void(*triapi_Color4f) (float r, float g, float b, float a); enginesurface_Texture* (*staticGetTextureById)(int id); void(__fastcall* enginesurface_drawSetTextureRGBA)(void* pthis, int, int textureId, const char* data, int wide, int tall, qboolean hardwareFilter, qboolean hasAlphaChannel); void(__fastcall* enginesurface_drawSetTexture)(void* pthis, int, int textureId);