Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Looking for a nice variable sigma gaussian blur shader  (Read 3620 times)

0 Members and 2 Guests are viewing this topic.

quinor

  • Newbie
  • *
  • Posts: 7
    • View Profile
Looking for a nice variable sigma gaussian blur shader
« on: January 12, 2017, 02:27:01 am »
For my project I need a blur shader. It has to have per-pass adjustable intensity in very wide range (sigma from 1 to 500) and becouse of some deeper mathematical reasons I would really prefer to have gaussian blur (well, discrete approximation of one) with actual sigma as control parameter.

I don't have enough experience to generate proper weights and pass them to a kernel yet. For now I am using crappy approximation of two passes of a moving average but it's very far from perfect.

I feel like this is such a standard problem that somebody must have had written it before and probably posted somewhere over the internet. If you happen to have such a piece of code stashed somewhere, it would be wery helpful if you've posted it to me or pointed in the right direction.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Looking for a nice variable sigma gaussian blur shader
« Reply #1 on: January 12, 2017, 04:09:28 am »
You could try searching around Shadertoy.

quinor

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Looking for a nice variable sigma gaussian blur shader
« Reply #2 on: January 12, 2017, 12:09:17 pm »
Thanks, I've found some very helpful things there :)

How much faster will it be in GLSL to pass an array of gaussian blur weights instead of computing them at runtime (it's 1/(2pi*sigma)*exp(-x^2/(sigma^2)),  so quite a compllicated function)?

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Looking for a nice variable sigma gaussian blur shader
« Reply #3 on: January 12, 2017, 08:21:21 pm »
Realistically, there are too many variables at play to accurately make a guess (amount of weights, CPU specs, GPU specs, etc).

If I had to make a guess, it'd be that the difference would be negligible; I would just calculate at runtime for ease of use.

Best way would be to profile the two methods yourself.

quinor

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Looking for a nice variable sigma gaussian blur shader
« Reply #4 on: January 13, 2017, 01:03:15 pm »
If anybody needed it:

#version 130

uniform sampler2D tex;
uniform float sigma;

#define gauss_constant 0.3989422804014327

float gauss_weight(int d)
{
  return (gauss_constant/sigma)*exp(-d*d/(2*sigma*sigma));
}

void main()
{
  vec3 sum = vec3(0.0);
  float delta = 1.0/textureSize(tex, 0).x;
  float div = 0;
  sum += gauss_weight(0)*texture(tex, gl_TexCoord[0].xy).rgb;
  div += gauss_weight(0);
  for (int i=1; i<=3*sigma; i++)
  {
    sum += gauss_weight(i)*texture(tex, gl_TexCoord[0].xy+vec2(delta*i, 0)).rgb;
    sum += gauss_weight(i)*texture(tex, gl_TexCoord[0].xy+vec2(-delta*i, 0)).rgb;
    div += 2*gauss_weight(i);
  }
  gl_FragColor = vec4(sum, 1.0)/div;
}
 

And in sfml:
frag.setUniform("sigma", (float)sigma_input.get_data());
frag.setUniform("tex", in.get_data());
 

Though it slows down a bit (800ms! per x and y pass on sigma=200) so I'll be experimenting with speeding this up.


EDIT:

Indeed, computation of the gaussian weight isn't important - eliminating it doesn't change computation time pretty much at all. The blur isn't ideal (average of the gaussian function over one unit of distance isn't exactly the value in the middle of said distance) but it's much closer to the real one and suits my application.
« Last Edit: January 13, 2017, 01:17:38 pm by quinor »

 

anything