Sign in to follow this  

[HLSL] Gaussian Blur Shader

Recommended Posts

I must have looked at a dozen examples for the gaussian blur shader, but I can't seem to figure out what I'm doing wrong in mine. I'm taking 13 nearby samples and blending them together. I don't intrinsically understand HLSL, so it's very possible that I don't quite "get" how the data is actually being passed into/out of my function. Can someone tell me what I'm doing wrong here? I've looked at other blur examples which are just as simple, so I don't think I'm missing any steps. All my weights add up to 1(-ish). Here is my code:

texture Source;
float Kernel[13] = {-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6};

static const float Weights[13] = { 0.002216, 0.008764, 0.026995, 0.064759, 0.120985, 0.176033, 0.199471, 0.176033, 0.120985, 0.064759, 0.026995, 0.008764, 0.002216};

sampler2D postTex = sampler_state
texture = <Source>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;

void BlurX_PS(in float2 tex : TEXCOORD0, out float4 dif : COLOR0)
dif = float4(0.0, 0.0, 0.0, 0.0);
float2 coord;

coord.y = tex.y;

for(int i = 0; i < 13; ++i)
coord.x = tex.x + Kernel[i];
dif += tex2D(postTex, coord.xy) * Weights[i];

Yes I am compiling the shader in a technique, I just left it off to save space.

I'm using this as a pixel shader for post-processing. I have my entire scene rendered to a texture, then I'm passing that texture through here when I render it to a quad which fills the screen.

EDIT: My problem appears to be that I can't sample pixels other than the one I am currently working with.

Share this post

Link to post
Share on other sites
Texture coordinates use the range [0, 1]. So 0 is the very left/top of the texture, and 1 is the very right/bottom. In your loop you're adding integer values to the texture coordinate in an attempt to move over by that number of texels, which is wrong. You need to divide the number of texels by the width or height of the texture (width for you horizontal pass, height for the vertical pass) to convert those offsets to UV space.

Also in the future, it helps to say what the actual problem is or what results you're getting. In most cases you can't just look at code and immediately know what's wrong with it, although this case is an exception. :P

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