Jump to content
  • Advertisement
Sign in to follow this  
Mr Furious

Neighbor Pixels in HLSL

This topic is 4908 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

I've been working on doing post processing effects lately and wanted to start doing filters. However, filters needs to sample their neighboring pixels. I know i could hack it by playing with arbitrary float2's until it looks ok but i'd rather be legit about it. For reference, i was looking at some of humus' code. Here's an example filter: (Edge detection using Sobel's method)
const float off;

float4 main(float2 texCoord: TEXCOORD) : COLOR {
	// Sobel filter
	float4 s00 = tex2D(Image, texCoord + float2(-off, -off));
	float4 s01 = tex2D(Image, texCoord + float2( 0,   -off));
	float4 s02 = tex2D(Image, texCoord + float2( off, -off));

	float4 s10 = tex2D(Image, texCoord + float2(-off,  0));
	float4 s12 = tex2D(Image, texCoord + float2( off,  0));

	float4 s20 = tex2D(Image, texCoord + float2(-off,  off));
	float4 s21 = tex2D(Image, texCoord + float2( 0,    off));
	float4 s22 = tex2D(Image, texCoord + float2( off,  off));

	float4 sobelX = s00 + 2 * s10 + s20 - s02 - 2 * s12 - s22;
	float4 sobelY = s00 + 2 * s01 + s02 - s20 - 2 * s21 - s22;

	float4 edgeSqr = sobelX * sobelX + sobelY * sobelY;
	return 1 - dot(edgeSqr, 80);
So i guess my question is how to properly compute the "off". He does it like this: renderer->changeShaderConstant3f("off", 0.13f / length(position)); But I don't understanding how he gets this.

Share this post

Link to post
Share on other sites
Original post by Namethatnobodyelsetook
They should be 1.0/texturewidth and 1.0/textureheight to give you neighbour pixels.

Thanks for the feedback, that seems a really intuitive way to go about it but it's deconstructing humus's work that's so elusive.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!