Jump to content
  • Advertisement
Sign in to follow this  
Julian Spillane

Optimizing My Edge-Detection Filter

This topic is 4726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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);

Share this post


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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!