try to improve motion blur

This commit is contained in:
milek7
2020-12-01 01:45:48 +01:00
parent c004047276
commit ee5551a424
4 changed files with 32 additions and 66 deletions

View File

@@ -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);

View File

@@ -439,12 +439,16 @@ bool opengl33_renderer::init_viewport(viewport_config &vp)
vp.main_tex = std::make_unique<opengl_texture>();
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<opengl_texture>();
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<gl::framebuffer>();
vp.main_fb->attach(*vp.main_tex, GL_COLOR_ATTACHMENT0);
vp.main_texv = std::make_unique<opengl_texture>();
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
{

View File

@@ -219,6 +219,7 @@ class opengl33_renderer : public gfx_renderer {
// msaa resolve buffer (when using motion blur)
std::unique_ptr<gl::framebuffer> main_fb;
std::unique_ptr<opengl_texture> main_texv;
std::unique_ptr<opengl_texture> main_texd;
std::unique_ptr<opengl_texture> main_tex;
// final HDR buffer (also serving as msaa resolve buffer when not using motion blur)

View File

@@ -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 <common>
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);
float depth_diff = 0.001 * 8.0;
vec4 result = vec4(0.0);
result += vec4(texture(color_tex, f_coords).rgb, 1.0);
vec4 oResult = texture(color_tex, f_coords);
for (int i = 1; i < nSamples; ++i)
{
vec2 offset = velocity * (float(i) / float(nSamples - 1) - 0.5);
oResult += texture(color_tex, f_coords + offset);
}
oResult /= float(nSamples);
out_color = oResult;
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);
}
out_color = vec4((result.rgb / result.w), 1.0);
}