diff --git a/Globals.cpp b/Globals.cpp index 6ffb67f1..8702b275 100644 --- a/Globals.cpp +++ b/Globals.cpp @@ -873,59 +873,6 @@ global_settings::ConfigParse(cParser &Parser) { Parser.getTokens(1); Parser >> gfx_postfx_motionblur_enabled; } - else if (token == "gfx.postfx.motionblur.shutter") - { - Parser.getTokens(1); - Parser >> gfx_postfx_motionblur_shutter; - } - else if (token == "gfx.postfx.motionblur.format") - { - Parser.getTokens(1); - std::string token; - Parser >> token; - if (token == "rg16f") - gfx_postfx_motionblur_format = GL_RG16F; - else if (token == "rg32f") - gfx_postfx_motionblur_format = GL_RG32F; - } - else if (token == "gfx.format.color") - { - Parser.getTokens(1); - std::string token; - Parser >> token; - if (token == "rgb8") - gfx_format_color = GL_RGB8; - else if (token == "rgb16f") - gfx_format_color = GL_RGB16F; - else if (token == "rgb32f") - gfx_format_color = GL_RGB32F; - else if (token == "r11f_g11f_b10f") - gfx_format_color = GL_R11F_G11F_B10F; - } - else if (token == "gfx.format.depth") - { - Parser.getTokens(1); - std::string token; - Parser >> token; - if (token == "z16") - gfx_format_depth = GL_DEPTH_COMPONENT16; - else if (token == "z24") - gfx_format_depth = GL_DEPTH_COMPONENT24; - else if (token == "z32") - gfx_format_depth = GL_DEPTH_COMPONENT32; - else if (token == "z32f") - gfx_format_depth = GL_DEPTH_COMPONENT32F; - } - else if (token == "gfx.skippipeline") - { - Parser.getTokens(1); - Parser >> gfx_skippipeline; - } - else if (token == "gfx.extraeffects") - { - Parser.getTokens(1); - Parser >> gfx_extraeffects; - } else if (token == "gfx.usegles") { Parser.getTokens(1); diff --git a/opengl33renderer.cpp b/opengl33renderer.cpp index 58dd66bb..0473778a 100644 --- a/opengl33renderer.cpp +++ b/opengl33renderer.cpp @@ -424,7 +424,7 @@ bool opengl33_renderer::init_viewport(viewport_config &vp) vp.msaa_rbc->alloc(Global.gfx_format_color, vp.width, vp.height, samples); vp.msaa_rbd = std::make_unique(); - vp.msaa_rbd->alloc(Global.gfx_format_depth, vp.width, vp.height, samples); + vp.msaa_rbd->alloc(Global.gfx_format_depth, vp.width, vp.height, samples); vp.msaa_fb = std::make_unique(); vp.msaa_fb->attach(*vp.msaa_rbc, GL_COLOR_ATTACHMENT0); @@ -437,14 +437,18 @@ bool opengl33_renderer::init_viewport(viewport_config &vp) vp.msaa_fb->attach(*vp.msaa_rbv, GL_COLOR_ATTACHMENT1); vp.main_tex = std::make_unique(); - vp.main_tex->alloc_rendertarget(Global.gfx_format_color, GL_RGB, vp.width, vp.height, 1, 1, GL_CLAMP_TO_EDGE); + vp.main_tex->alloc_rendertarget(Global.gfx_format_color, GL_RGB, vp.width, vp.height, 1, 1, GL_CLAMP_TO_EDGE); + + vp.main_texd = std::make_unique(); + vp.main_texd->alloc_rendertarget(Global.gfx_format_depth, GL_DEPTH_COMPONENT, vp.width, vp.height, 1, 1, GL_CLAMP_TO_EDGE); vp.main_fb = std::make_unique(); vp.main_fb->attach(*vp.main_tex, GL_COLOR_ATTACHMENT0); vp.main_texv = std::make_unique(); - vp.main_texv->alloc_rendertarget(Global.gfx_postfx_motionblur_format, GL_RG, vp.width, vp.height); + vp.main_texv->alloc_rendertarget(Global.gfx_postfx_motionblur_format, GL_RG, vp.width, vp.height); vp.main_fb->attach(*vp.main_texv, GL_COLOR_ATTACHMENT1); + vp.main_fb->attach(*vp.main_texd, GL_DEPTH_ATTACHMENT); vp.main_fb->setup_drawing(2); if( !vp.main_fb->is_complete() ) { @@ -874,10 +878,11 @@ void opengl33_renderer::Render_pass(viewport_config &vp, rendermode const Mode) vp.main_fb->clear(GL_COLOR_BUFFER_BIT); vp.msaa_fb->blit_to(vp.main_fb.get(), vp.width, vp.height, GL_COLOR_BUFFER_BIT, GL_COLOR_ATTACHMENT0); vp.msaa_fb->blit_to(vp.main_fb.get(), vp.width, vp.height, GL_COLOR_BUFFER_BIT, GL_COLOR_ATTACHMENT1); + vp.msaa_fb->blit_to(vp.main_fb.get(), vp.width, vp.height, GL_DEPTH_BUFFER_BIT, GL_DEPTH_ATTACHMENT); model_ubs.param[0].x = m_framerate / (1.0 / Global.gfx_postfx_motionblur_shutter); model_ubo->update(model_ubs); - m_pfx_motionblur->apply({vp.main_tex.get(), vp.main_texv.get()}, vp.main2_fb.get()); + m_pfx_motionblur->apply({vp.main_tex.get(), vp.main_texv.get(), vp.main_texd.get()}, vp.main2_fb.get()); } else { diff --git a/opengl33renderer.h b/opengl33renderer.h index 56f5e2d1..ee00852f 100644 --- a/opengl33renderer.h +++ b/opengl33renderer.h @@ -219,6 +219,7 @@ class opengl33_renderer : public gfx_renderer { // msaa resolve buffer (when using motion blur) std::unique_ptr main_fb; std::unique_ptr main_texv; + std::unique_ptr main_texd; std::unique_ptr main_tex; // final HDR buffer (also serving as msaa resolve buffer when not using motion blur) diff --git a/shaders/postfx_motionblur.frag b/shaders/postfx_motionblur.frag index 331fc0d7..146179ac 100644 --- a/shaders/postfx_motionblur.frag +++ b/shaders/postfx_motionblur.frag @@ -7,23 +7,36 @@ uniform sampler2D color_tex; #texture (velocity_tex, 1, RG) uniform sampler2D velocity_tex; +#texture (depth_tex, 2, R) +uniform sampler2D depth_tex; + #include void main() -{ +{ + float ori_depth = texture(depth_tex, f_coords).x; + vec2 texelSize = 1.0 / vec2(textureSize(color_tex, 0)); - vec2 velocity = texture(velocity_tex, f_coords).rg * param[0].r; + vec2 velocity = texture(velocity_tex, f_coords).xy * param[0].x; float speed = length(velocity / texelSize); - int nSamples = clamp(int(speed), 1, 64); + int nSamples = clamp(int(speed), 1, 32); - vec4 oResult = texture(color_tex, f_coords); + float depth_diff = 0.001 * 8.0; + vec4 result = vec4(0.0); + + result += vec4(texture(color_tex, f_coords).rgb, 1.0); + for (int i = 1; i < nSamples; ++i) { vec2 offset = velocity * (float(i) / float(nSamples - 1) - 0.5); - oResult += texture(color_tex, f_coords + offset); + + vec2 coord = f_coords + offset; + float depth = texture(depth_tex, coord).x; + + if (abs(depth - ori_depth) < depth_diff) + result += vec4(texture(color_tex, coord).rgb, 1.0); } - oResult /= float(nSamples); - - out_color = oResult; -} + + out_color = vec4((result.rgb / result.w), 1.0); +}