Sign in to follow this  
Julian Spillane

Optimizing My Edge-Detection Filter

Recommended Posts

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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this