Optimizing My Edge-Detection Filter

Started by
3 comments, last by Julian Spillane 18 years, 9 months ago
Hey all. I have been tinkering around with adding some post-processing effects into my engine, and everything's been running smoothly until I hit the edge detection filter. The frame I hit I take from using that filter is horrendous, and I know that the lag is in the shader (for information I'm using GLSL). Here's my shader code as of now:

uniform sampler2D tex2D;

void main()
{
   const float offset = 1.0 / 256.0;
   vec2 texCoord = gl_TexCoord[0].xy;

   vec4 c  = texture2D(tex2D, texCoord);
   vec4 bl = texture2D(tex2D, texCoord + vec2(-offset, -offset));
   vec4 l  = texture2D(tex2D, texCoord + vec2(-offset,     0.0));
   vec4 tl = texture2D(tex2D, texCoord + vec2(-offset,  offset));
   vec4 t  = texture2D(tex2D, texCoord + vec2(    0.0,  offset));
   vec4 ur = texture2D(tex2D, texCoord + vec2( offset,  offset));
   vec4 r  = texture2D(tex2D, texCoord + vec2( offset,     0.0));
   vec4 br = texture2D(tex2D, texCoord + vec2( offset, -offset));
   vec4 b  = texture2D(tex2D, texCoord + vec2(    0.0, -offset));

   gl_FragColor = 8.0 * (c + -0.125 * (bl + l + tl + t + ur + r + br + b));
}
Now, it seems that just the declaration of so many variables is really killing any speed I might otherwise have. Do you guys have any ideas as to how I could optimize it at all? Thanks a lot. -Julian Spillane
Advertisement
Not a whole lot of optimization you can do there.. to avoid making so many variables, just add up the colors as you fetch them, instead of putting them in temps and then adding them at the end. Something like

vec4 c = texture2D(tex2D, texCoord);
vec4 edge = texture2D(tex2D, texCoord + vec2(-offset, -offset)) +
texture2D(tex2D, texCoord + vec2(-offset, 0.0)) +
texture2D(tex2D, texCoord + vec2(-offset, offset)) +
texture2D(tex2D, texCoord + vec2( 0.0, offset)) +
texture2D(tex2D, texCoord + vec2( offset, offset)) +
texture2D(tex2D, texCoord + vec2( offset, 0.0)) +
texture2D(tex2D, texCoord + vec2( offset, -offset)) +
texture2D(tex2D, texCoord + vec2( 0.0, -offset));

gl_FragColor = 8.0 * (c + -0.125 * edge);
Thanks for the reply. Yeah, I didn't think of inlining it like that. That should speed it up a little (at least save on the memory overhead). I just wish I could be even more efficient.

Oh well, thanks a lot! :)

rate++
In general, you should use as few calculated texture coordiantes as possible.

If you can pass in pre-offset texture coordinates from the vertex shader, that should reduce the # of instructions in your pixel shader, and speed things up.

You can pack in 2 offsets per iterator, so you should be able to do all of your offsets in the vertex shader, and just unpack the offsets in your pixel shader as needed. Also, you can skip the zero offsets as well.
Hey, thanks a lot! That really sped up my shader.

I should have known not to do so many calculations in the fragment shader.

Thanks a lot.

This topic is closed to new replies.

Advertisement