optimized blurs
This commit is contained in:
+18
-14
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -9,5 +9,5 @@
|
||||
<row name="Vertical">
|
||||
<field type="bool" default="1" id="vert_blur"/>
|
||||
</row>
|
||||
<shader vert="common.vert" frag="boxblur.frag"/>
|
||||
<shader vert="common.vert" frag="boxblur.frag" iterations="2"/>
|
||||
</effect>
|
||||
+26
-20
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<effect name="Gaussian Blur" category="Blur">
|
||||
<row name="Radius">
|
||||
<!-- <row name="Radius">
|
||||
<field type="double" min="0" default="10" id="radius"/>
|
||||
</row>
|
||||
</row> -->
|
||||
<row name="Sigma">
|
||||
<field type="double" min="0" default="5.5" id="sigma"/>
|
||||
</row>
|
||||
@@ -12,8 +12,5 @@
|
||||
<row name="Vertical">
|
||||
<field type="bool" default="1" id="vert_blur"/>
|
||||
</row>
|
||||
<!-- <row name="Opt">
|
||||
<field type="bool" default="0" id="opt"/>
|
||||
</row> -->
|
||||
<shader vert="common.vert" frag="gaussianblur.frag"/>
|
||||
<shader vert="common.vert" frag="gaussianblur.frag" iterations="2"/>
|
||||
</effect>
|
||||
+14
-2
@@ -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;i<rows.size();i++) {
|
||||
EffectRow* row = rows.at(i);
|
||||
|
||||
+2
-1
@@ -193,7 +193,7 @@ public:
|
||||
const char* ffmpeg_filter;
|
||||
|
||||
virtual void process_image(double timecode, uint8_t* input, uint8_t* output, int size);
|
||||
virtual void process_shader(double timecode, GLTextureCoords&);
|
||||
virtual void process_shader(double timecode, GLTextureCoords&, int iteration);
|
||||
virtual void process_coords(double timecode, GLTextureCoords& coords, int data);
|
||||
virtual GLuint process_superimpose(double timecode);
|
||||
virtual void process_audio(double timecode_start, double timecode_end, quint8* samples, int nb_bytes, int channel_count);
|
||||
@@ -231,6 +231,7 @@ private:
|
||||
QGridLayout* ui_layout;
|
||||
QWidget* ui;
|
||||
bool bound;
|
||||
int iterations;
|
||||
|
||||
// superimpose functions
|
||||
virtual void redraw(double timecode);
|
||||
|
||||
@@ -97,9 +97,11 @@ void process_effect(Clip* c,
|
||||
if (can_process_shaders || e->enable_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;i<e->getIterations();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);
|
||||
|
||||
Reference in New Issue
Block a user