From 8f9a3f37e3cadd636d2d628e1e4c255e6cb8a940 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Wed, 30 Jan 2019 11:19:30 +1100 Subject: [PATCH] optimized blurs --- effects/boxblur.frag | 32 +++++++++++++++------------ effects/boxblur.xml | 2 +- effects/gaussianblur.frag | 46 ++++++++++++++++++++++----------------- effects/gaussianblur.xml | 9 +++----- project/effect.cpp | 16 ++++++++++++-- project/effect.h | 3 ++- ui/renderfunctions.cpp | 8 ++++--- 7 files changed, 69 insertions(+), 47 deletions(-) diff --git a/effects/boxblur.frag b/effects/boxblur.frag index 626cc1f7e..3da0380bc 100644 --- a/effects/boxblur.frag +++ b/effects/boxblur.frag @@ -6,24 +6,28 @@ uniform float radius; uniform vec2 resolution; uniform bool horiz_blur; uniform bool vert_blur; +uniform int iteration; + +varying vec2 vTexCoord; void main(void) { float rad = ceil(radius); - float x_rad = (horiz_blur) ? rad : 0.5; - float y_rad = (vert_blur) ? rad : 0.5; - vec2 texCoord = gl_FragCoord.xy/resolution; - if (radius == 0.0 || (!horiz_blur && !vert_blur)) { - gl_FragColor = texture2D(image, texCoord); - } else { - float divider = 1.0; - if (horiz_blur) divider /= rad; - if (vert_blur) divider /= rad; - vec4 color = vec4(0.0); - for (float x=-x_rad+0.5;x<=x_rad;x+=2.0) { - for (float y=-y_rad+0.5;y<=y_rad;y+=2.0) { - color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y+y))/resolution)*(divider); - } + + float divider = 1.0 / rad; + vec4 color = vec4(0.0); + bool radius_is_zero = (rad == 0.0); + + if (iteration == 0 && horiz_blur && !radius_is_zero) { + for (float x=-rad+0.5;x<=rad;x+=2.0) { + color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y))/resolution)*(divider); } gl_FragColor = color; + } else if (iteration == 1 && vert_blur && !radius_is_zero) { + for (float x=-rad+0.5;x<=rad;x+=2.0) { + color += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y+x))/resolution)*(divider); + } + gl_FragColor = color; + } else { + gl_FragColor = texture2D(image, vTexCoord); } } \ No newline at end of file diff --git a/effects/boxblur.xml b/effects/boxblur.xml index 1dd4cb45e..a6c0cb491 100644 --- a/effects/boxblur.xml +++ b/effects/boxblur.xml @@ -9,5 +9,5 @@ - + \ No newline at end of file diff --git a/effects/gaussianblur.frag b/effects/gaussianblur.frag index b5dd4e8c9..80aaedb18 100644 --- a/effects/gaussianblur.frag +++ b/effects/gaussianblur.frag @@ -4,13 +4,14 @@ uniform sampler2D image; -uniform float radius; +// uniform float radius; uniform float sigma; uniform vec2 resolution; uniform bool horiz_blur; uniform bool vert_blur; +uniform int iteration; -uniform bool opt; +varying vec2 vTexCoord; float gaussian(float x, float sigma) { return (1.0/(sigma*sqrt(2.0*M_PI)))*exp(-0.5*pow(x/sigma, 2.0)); @@ -21,28 +22,33 @@ float gaussian2(float x, float y, float sigma) { } void main(void) { - if (radius == 0.0 || sigma == 0.0 || (!horiz_blur && !vert_blur)) { - gl_FragColor = texture2D(image, gl_FragCoord.xy/resolution); - } else { - float rad = ceil(radius); - float x_rad = horiz_blur ? rad : 0.5; - float y_rad = vert_blur ? rad : 0.5; + float rad = ceil(sigma); - float sum = 0.0; + float sum = 0.0; - for (float x=-x_rad+0.5;x<=x_rad;x+=2.0) { - for (float y=-y_rad+0.5;y<=y_rad;y+=2.0) { - sum += gaussian2(x, y, sigma); - } + vec4 color = vec4(0.0); + + bool radius_is_zero = (rad == 0.0); + + if (!radius_is_zero) { + for (float x=-rad+0.5;x<=rad;x+=2.0) { + sum += gaussian2(x, 0.0, sigma); } + } - vec4 color = vec4(0.0); - for (float x=-x_rad+0.5;x<=x_rad;x+=2.0) { - for (float y=-y_rad+0.5;y<=y_rad;y+=2.0) { - float weight = (gaussian2(x, y, sigma)/sum); - color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y+y))/resolution)*(weight); - } + if (iteration == 0 && horiz_blur && !radius_is_zero) { + for (float x=-rad+0.5;x<=rad;x+=2.0) { + float weight = (gaussian2(x, 0.0, sigma)/sum); + color += texture2D(image, (vec2(gl_FragCoord.x+x, gl_FragCoord.y))/resolution)*(weight); } gl_FragColor = color; - } + } else if (iteration == 1 && vert_blur && !radius_is_zero) { + for (float x=-rad+0.5;x<=rad;x+=2.0) { + float weight = (gaussian2(0.0, x, sigma)/sum); + color += texture2D(image, (vec2(gl_FragCoord.x, gl_FragCoord.y+x))/resolution)*(weight); + } + gl_FragColor = color; + } else { + gl_FragColor = texture2D(image, vTexCoord); + } } \ No newline at end of file diff --git a/effects/gaussianblur.xml b/effects/gaussianblur.xml index 76a6bc82a..d0d669f6d 100644 --- a/effects/gaussianblur.xml +++ b/effects/gaussianblur.xml @@ -1,8 +1,8 @@ - + @@ -12,8 +12,5 @@ - - + \ No newline at end of file diff --git a/project/effect.cpp b/project/effect.cpp index 2beee31f7..7bb583bb4 100644 --- a/project/effect.cpp +++ b/project/effect.cpp @@ -102,7 +102,8 @@ Effect::Effect(Clip* c, const EffectMeta *em) : texture(nullptr), enable_always_update(false), isOpen(false), - bound(false) + bound(false), + iterations(1) { // set up base UI container = new CollapsibleWidget(); @@ -276,6 +277,8 @@ Effect::Effect(Clip* c, const EffectMeta *em) : vertPath = attr.value().toString(); } else if (attr.name() == "frag") { fragPath = attr.value().toString(); + } else if (attr.name() == "iterations") { + setIterations(attr.value().toInt()); } } }/* else if (reader.name() == "superimpose" && reader.isStartElement()) { @@ -695,6 +698,14 @@ void Effect::endEffect() { bound = false; } +int Effect::getIterations() { + return iterations; +} + +void Effect::setIterations(int i) { + iterations = i; +} + void Effect::process_image(double, uint8_t *, uint8_t *, int){} Effect* Effect::copy(Clip* c) { @@ -704,9 +715,10 @@ Effect* Effect::copy(Clip* c) { return copy; } -void Effect::process_shader(double timecode, GLTextureCoords&) { +void Effect::process_shader(double timecode, GLTextureCoords&, int iteration) { glslProgram->setUniformValue("resolution", parent_clip->getWidth(), parent_clip->getHeight()); glslProgram->setUniformValue("time", GLfloat(timecode)); + glslProgram->setUniformValue("iteration", iteration); for (int i=0;ienable_superimpose) { e->startEffect(); if (can_process_shaders && e->is_glsl_linked()) { - e->process_shader(timecode, coords); - composite_texture = draw_clip(c->fbo[fbo_switcher], composite_texture, true); - fbo_switcher = !fbo_switcher; + for (int i=0;igetIterations();i++) { + e->process_shader(timecode, coords, i); + composite_texture = draw_clip(c->fbo[fbo_switcher], composite_texture, true); + fbo_switcher = !fbo_switcher; + } } if (e->enable_superimpose) { GLuint superimpose_texture = e->process_superimpose(timecode);