Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pixel Shader Gaussian Blur

Started by Debonaire Sep 3, 2008 at 11:15 PM 2 replies 14.7k views
Original Post
Debonaire
Debonaire
hi everyone i'm trying to implement some kind of 3x3 box filter but it seems it's not getting the blur i want. I tried passing my image several 4 times on the filter but the banding is still noticeable. I'd like to try out gaussian blur. Can anyone point me out to a sample pixel shader implementation? I kinda found some coefficient values matrix from wikipedia but I'm not clear on how to use it. Any advice would help. Thanks so much! D
Shirakana2
Shirakana2
Here is a sample of the function (glsl implementation) I use for a 3x3 gaussian blur:

vec4 GaussianBlur() {	vec4 result = 0.0;	vec2 texCoord;		mat3 gaussianCoef = {		1.0,	2.0,	1.0,		2.0,	4.0,	2.0,		1.0,	2.0,	1.0};		// stepU = 1.0 / screenResolutionWidth | stepV = 1.0 / screenResolutionHeight	for(int i=0;i<3;i++) {		for(int j=0;j<3;j++) {			texCoord = gl_TexCoord[0].xy + vec2((i-1)*stepU,(j-1)*stepV);			result += gaussianCoef[j] * texture2D(myTexture,texCoord);		}	}	result /= 16.0;	return result;}


The gaussian kernel is quite basic so you may find one adaptated to what you want to blur. If you plan to make a bigger kernel (5x5, 7x7...) I recommend you to do it in 2 pass: one pass or horizontal blur, and one pass of vertical blur of the result from previous pass. It will be much more efficient.
Some of my previous work on my personal webpage
unexist
unexist
http://prideout.net/bloom/index.php ..

basically you use the pascal's triangle (see link) to figure out what your weights are based on how many samples you want to use for each texel. Then apply those weights to your samples in a horizontal pass and then again in a vertical pass.

//Blur Weightsstatic const float BlurWeights[cKernelSize] = {	1.0f / 4096.0f,	12.0f / 4096.0f,	66.0f / 4096.0f,	220.0f / 4096.0f,	495.0f / 4096.0f,	792.0f / 4096.0f,	924.0f / 4096.0f,	792.0f / 4096.0f,	495.0f / 4096.0f,	220.0f / 4096.0f,	66.0f / 4096.0f,	12.0f / 4096.0f,	1.0f / 4096.0f,}; //Horizontal Blurfloat4 HorizontalBlurPS(float2 uv : TEXCOORD1 ) : COLOR0{    float4 Color = 0;        for (int i = 0; i < cKernelSize; i++)    {            Color += tex2D( g_SampTex, uv + BlurOffsetsH.xy ) * BlurWeights;    }    return Color;}//Vertical Blurfloat4 VerticalBlurPS(float2 uv : TEXCOORD1 ) : COLOR0{    float4 Color = 0;    for (int i = 0; i < cKernelSize; i++)    {        Color += tex2D( g_SampTex, uv + BlurOffsetsV.xy ) * BlurWeights;    }    return Color;}
Debonaire
Debonaire
Awesome, thanks so much for the info guys. I really appreciate it. It works great.

Thanks again Shirakana2 and unexist :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.