From 9e57bfb73f9bf4eb4b03d45841d12a9ad53f4d60 Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 30 Jan 2018 13:42:50 +0800 Subject: [PATCH] Split point light scatter so it can be combined together --- data/shaders/combine_diffuse_color.frag | 12 +++++++- src/graphics/lighting_passes.cpp | 19 ++++++------- src/graphics/lighting_passes.hpp | 4 --- src/graphics/post_processing.cpp | 4 +++ src/graphics/rtts.cpp | 3 ++ src/graphics/shader_based_renderer.cpp | 38 +++++++++++++------------ 6 files changed, 47 insertions(+), 33 deletions(-) diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index df7f34e86..41a259dd9 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -4,6 +4,7 @@ uniform sampler2D ssao_tex; uniform sampler2D gloss_map; uniform sampler2D diffuse_color; uniform sampler2D depth_stencil; +uniform sampler2D light_scatter; out vec4 o_final_color; @@ -39,5 +40,14 @@ void main() vec3 fog = u_fog_color.xyz * factor; // Additively blend the color by fog - o_final_color = color_1 + vec4(fog, factor); + color_1 = color_1 + vec4(fog, factor); + + // Light scatter (alpha blend function: (GL_ONE, GL_ONE_MINUS_SRC_ALPHA)) + vec4 ls = texture(light_scatter, tc); + vec4 color_2; + color_2.r = ls.r + color_1.r * (1.0 - ls.a); + color_2.g = ls.g + color_1.g * (1.0 - ls.a); + color_2.b = ls.b + color_1.b * (1.0 - ls.a); + color_2.a = ls.a + color_1.a * (1.0 - ls.a); + o_final_color = color_2; } diff --git a/src/graphics/lighting_passes.cpp b/src/graphics/lighting_passes.cpp index b58f2f3b1..051a59b17 100644 --- a/src/graphics/lighting_passes.cpp +++ b/src/graphics/lighting_passes.cpp @@ -106,6 +106,11 @@ public: glVertexAttribDivisorARB(attrib_Color, 1); glVertexAttribDivisorARB(attrib_Radius, 1); } // PointLightShader + ~PointLightShader() + { + glDeleteBuffers(1, &vbo); + glDeleteVertexArrays(1, &vao); + } // PointLightShader }; // PointLightShader @@ -116,7 +121,6 @@ class PointLightScatterShader : public TextureShader { public: - GLuint vbo; GLuint vao; PointLightScatterShader() { @@ -157,6 +161,10 @@ public: glVertexAttribDivisorARB(attrib_Color, 1); glVertexAttribDivisorARB(attrib_Radius, 1); } // PointLightScatterShader + ~PointLightScatterShader() + { + glDeleteVertexArrays(1, &vao); + } // ~PointLightScatterShader }; // ============================================================================ @@ -465,7 +473,6 @@ void LightingPasses::renderLights( bool has_shadow, void LightingPasses::renderLightsScatter(GLuint depth_stencil_texture, const FrameBuffer& half1_framebuffer, const FrameBuffer& half2_framebuffer, - const FrameBuffer& colors_framebuffer, const PostProcessing* post_processing) { half1_framebuffer.bind(); @@ -505,14 +512,6 @@ void LightingPasses::renderLightsScatter(GLuint depth_stencil_texture, glDisable(GL_BLEND); post_processing->renderGaussian6Blur(half1_framebuffer, half2_framebuffer, 5., 5.); - glEnable(GL_BLEND); - - glDisable(GL_DEPTH_TEST); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - colors_framebuffer.bind(); - post_processing->renderPassThrough(half1_framebuffer.getRTT()[0], - colors_framebuffer.getWidth(), - colors_framebuffer.getHeight()); } // renderLightsScatter #endif diff --git a/src/graphics/lighting_passes.hpp b/src/graphics/lighting_passes.hpp index 4fd6bab43..522c2782b 100644 --- a/src/graphics/lighting_passes.hpp +++ b/src/graphics/lighting_passes.hpp @@ -55,11 +55,7 @@ public: void renderLightsScatter(GLuint depth_stencil_texture, const FrameBuffer& half1_framebuffer, const FrameBuffer& half2_framebuffer, - const FrameBuffer& colors_framebuffer, const PostProcessing* post_processing); - - - }; #endif //HEADER_LIGHTING_PASSES_HPP diff --git a/src/graphics/post_processing.cpp b/src/graphics/post_processing.cpp index c65ee3acf..8640a1ea2 100644 --- a/src/graphics/post_processing.cpp +++ b/src/graphics/post_processing.cpp @@ -905,11 +905,13 @@ void PostProcessing::renderGaussian6Blur(const FrameBuffer &in_fbo, if (!CVS->supportsComputeShadersFiltering()) { auxiliary.bind(); + glClear(GL_COLOR_BUFFER_BIT); Gaussian6VBlurShader::getInstance() ->render(in_fbo.getRTT()[0], in_fbo.getWidth(), in_fbo.getHeight(), sigma_v); in_fbo.bind(); + glClear(GL_COLOR_BUFFER_BIT); Gaussian6HBlurShader::getInstance()->setTextureUnits(auxiliary.getRTT()[0]); Gaussian6HBlurShader::getInstance()->render(auxiliary, in_fbo.getWidth(), in_fbo.getHeight(), sigma_h); @@ -959,9 +961,11 @@ void PostProcessing::renderHorizontalBlur(const FrameBuffer &in_fbo, in_fbo.getHeight() == auxiliary.getHeight()); auxiliary.bind(); + glClear(GL_COLOR_BUFFER_BIT); Gaussian6HBlurShader::getInstance()->render(in_fbo, in_fbo.getWidth(), in_fbo.getHeight(), 2.0f ); in_fbo.bind(); + glClear(GL_COLOR_BUFFER_BIT); Gaussian6HBlurShader::getInstance()->render(auxiliary, in_fbo.getWidth(), in_fbo.getHeight(), 2.0f); } // renderHorizontalBlur diff --git a/src/graphics/rtts.cpp b/src/graphics/rtts.cpp index 32a5c5ee5..7b78752f0 100644 --- a/src/graphics/rtts.cpp +++ b/src/graphics/rtts.cpp @@ -303,6 +303,9 @@ RTT::RTT(unsigned int width, unsigned int height, float rtt_scale, getFBO(FBO_HALF1_R).bind(); glClearColor(1., 1., 1., 1.); glClear(GL_COLOR_BUFFER_BIT); + getFBO(FBO_HALF1).bind(); + glClearColor(0., 0., 0., 0.); + glClear(GL_COLOR_BUFFER_BIT); } glBindFramebuffer(GL_FRAMEBUFFER, 0); } diff --git a/src/graphics/shader_based_renderer.cpp b/src/graphics/shader_based_renderer.cpp index ac47fdadb..1b6623b45 100644 --- a/src/graphics/shader_based_renderer.cpp +++ b/src/graphics/shader_based_renderer.cpp @@ -195,7 +195,7 @@ void ShaderBasedRenderer::renderShadows() } // ============================================================================ -class CombineDiffuseColor : public TextureShader +class CombineDiffuseColor : public TextureShader { public: CombineDiffuseColor() @@ -208,13 +208,14 @@ public: 2, "ssao_tex", ST_NEAREST_FILTERED, 3, "gloss_map", ST_NEAREST_FILTERED, 4, "diffuse_color", ST_NEAREST_FILTERED, - 5, "depth_stencil", ST_NEAREST_FILTERED); + 5, "depth_stencil", ST_NEAREST_FILTERED, + 6, "light_scatter", ST_NEAREST_FILTERED); } // CombineDiffuseColor // ------------------------------------------------------------------------ void render(GLuint dm, GLuint sm, GLuint st, GLuint gm, GLuint dc, - GLuint ds) + GLuint ds, GLuint lt) { - setTextureUnits(dm, sm, st, gm, dc, ds); + setTextureUnits(dm, sm, st, gm, dc, ds, lt); drawFullScreenEffect(); } // render }; // CombineDiffuseColor @@ -311,6 +312,19 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca PROFILER_POP_CPU_MARKER(); } + const Track * const track = Track::getCurrentTrack(); + // Render discrete lights scattering + if (track && track->isFogEnabled()) + { + PROFILER_PUSH_CPU_MARKER("- PointLight Scatter", 0xFF, 0x00, 0x00); + ScopedGPUTimer Timer(irr_driver->getGPUTimer(Q_FOG)); + m_lighting_passes.renderLightsScatter(m_rtts->getDepthStencilTexture(), + m_rtts->getFBO(FBO_HALF1), + m_rtts->getFBO(FBO_HALF2), + m_post_processing); + PROFILER_POP_CPU_MARKER(); + } + m_rtts->getFBO(FBO_COLORS).bind(); glClear(GL_COLOR_BUFFER_BIT); @@ -326,7 +340,8 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca m_rtts->getRenderTarget(RTT_HALF1_R), m_rtts->getRenderTarget(RTT_SP_GLOSS), m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR), - m_rtts->getDepthStencilTexture()); + m_rtts->getDepthStencilTexture(), + m_rtts->getRenderTarget(RTT_HALF1)); PROFILER_POP_CPU_MARKER(); } @@ -346,19 +361,6 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca PROFILER_POP_CPU_MARKER(); } - const Track * const track = Track::getCurrentTrack(); - // Render discrete lights scattering - if (track && track->isFogEnabled()) - { - PROFILER_PUSH_CPU_MARKER("- PointLight Scatter", 0xFF, 0x00, 0x00); - ScopedGPUTimer Timer(irr_driver->getGPUTimer(Q_FOG)); - m_lighting_passes.renderLightsScatter(m_rtts->getDepthStencilTexture(), - m_rtts->getFBO(FBO_HALF1), - m_rtts->getFBO(FBO_HALF2), - m_rtts->getFBO(FBO_COLORS), - m_post_processing); - PROFILER_POP_CPU_MARKER(); - } PROFILER_PUSH_CPU_MARKER("- Glow", 0xFF, 0xFF, 0x00); // Render anything glowing.