I was trying to write a very simple box blur shader, but it turned out to be awfully slow (3 FPS max), and I really can't imagine why...
I just can't think of a more simple and fast way to create a blur effect.
Here's the code:
uniform sampler2D texture;
uniform float width, height;
const float step_w = 1.0 / width;
const float step_h = 1.0 / height;
void main()
{
vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
float radius = 4.0, x, y;
float num = 0.0, cx, cy;
for(y = -radius; y < radius; y++)
for(x = -radius; x < radius; x++)
{
cx = x * step_w + gl_TexCoord[0].st[0];
cy = y * step_h + gl_TexCoord[0].st[1];
if(cx >= 0.0 && cy >= 0.0 && cx <= 1.0 && cy <= 1.0)
{
sum += texture2D(texture, vec2(cx, cy));
num += 1.0;
}
}
gl_FragColor = sum / num;
}
Here's an even faster implementation, but this one doesn't seem to work at all!
uniform sampler2D texture;
uniform float step_w, step_h;
void main()
{
vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
float radius = 4.0, x, y, minx, miny, maxx, maxy;
float num = 0.0, cx, cy;
minx = -radius * step_w;
miny = -radius * step_h;
maxx = -minx;
maxy = -miny;
for(y = miny; y < maxy; y += step_h)
for(x = minx; x < maxx; x += step_w)
{
cx = x + gl_TexCoord[0].st[0];
cy = y + gl_TexCoord[0].st[1];
if(cx >= 0.0 && cy >= 0.0 && cx <= 1.0 && cy <= 1.0)
{
sum += texture2D(texture, vec2(cx, cy));
num += 1.0;
}
}
gl_FragColor = sum / num;
}
Why is the first shader so slow, and why doesn't the second work at all? O_O