Original Post
Hi,
I've been trying to implement a Gaussian Blur shader in HLSL to use in DirectX 10.
I don't seem to get correct results, or not completely correct.
Can anyone check if I'm doing things correctly?
I think that I miss something, but I don't know what ^^
This is what I do now:
I've been trying to implement a Gaussian Blur shader in HLSL to use in DirectX 10.
I don't seem to get correct results, or not completely correct.
Can anyone check if I'm doing things correctly?
I think that I miss something, but I don't know what ^^
This is what I do now:
// -------------------------------------------------------------
// Texture & Sampler
// -------------------------------------------------------------
texture texTexture : TEXTURE0;
sampler sampTexture = sampler_state {
Texture = (texTexture);
AddressU = Wrap;
AddressV = Wrap;
};
// -------------------------------------------------------------
// Parameters
// -------------------------------------------------------------
float centerTapWeight;
const float2 texelSize;
const float tapOffSets[4] = {1, 2, 3, 4};
const float PI = 3.14159265f;
const float e = 2.71828182f;
float4 tapWeights;
const float standardDeviation = 2.0f;
// -------------------------------------------------------------
// Input/Output channels
// -------------------------------------------------------------
struct VS_INPUT
{
float4 position : POSITION;
float2 texCoord : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 position : POSITION;
float2 centerTap : TEXCOORD0;
float4 positiveTaps[2] : TEXCOORD1;
float4 negativeTaps[2] : TEXCOORD3;
};
#define PS_INPUT VS_OUTPUT
// -------------------------------------------------------------
// Vertex Shader
// -------------------------------------------------------------
VS_OUTPUT VS(const VS_INPUT IN, uniform float2 DIRECTION)
{
VS_OUTPUT OUT;
OUT.position = IN.position;
OUT.centerTap = IN.texCoord;
for(int i = 0; i < 2; ++i)
{
//OUT.taps = IN.texCoord + tapOffSets * DIRECTION * texelSize;
OUT.positiveTaps.xy = IN.texCoord + tapOffSets[i*2] * DIRECTION * texelSize;
OUT.negativeTaps.xy = IN.texCoord - tapOffSets[i*2] * DIRECTION * texelSize;
OUT.positiveTaps.zw = IN.texCoord + tapOffSets[i*2+1] * DIRECTION * texelSize;
OUT.negativeTaps.zw = IN.texCoord - tapOffSets[i*2+1] * DIRECTION * texelSize;
}
return OUT;
}
// -------------------------------------------------------------
// Pixel Shader function
// -------------------------------------------------------------
float4 PS(PS_INPUT IN) : COLOR
{
float firstPart = 1/sqrt(2*PI*standardDeviation);
float power = (-(pow(0, 2)))/(2*pow(standardDeviation, 2));
float secondPart = pow(e, power);
centerTapWeight = firstPart*secondPart;
//float4x4 samples;
float4x4 positiveSamples;
float4x4 negativeSamples;
float4 color = tex2D(sampTexture, IN.centerTap) * centerTapWeight;
for(int i = 0; i < 2; ++i)
{
//samples = tex2D(sampTexture, IN.taps);
positiveSamples[i*2] = tex2D(sampTexture, IN.positiveTaps.xy);
negativeSamples[i*2] = tex2D(sampTexture, IN.negativeTaps.xy);
positiveSamples[i*2+1] = tex2D(sampTexture, IN.positiveTaps.zw);
negativeSamples[i*2+1] = tex2D(sampTexture, IN.negativeTaps.zw);
float firstPart = 1/sqrt(2*PI*standardDeviation);
float power = (-(pow(IN.positiveTaps.xy, 2)))/(2*pow(standardDeviation, 2));
float secondPart = pow(e, power);
tapWeights[i*2] = firstPart*secondPart;
firstPart = 1/sqrt(2*PI*standardDeviation);
power = (-(pow(IN.positiveTaps.zw, 2)))/(2*pow(standardDeviation, 2));
secondPart = pow(e, power);
tapWeights[i*2+1] = firstPart*secondPart;
}
color += mul(tapWeights, positiveSamples) + mul(tapWeights, negativeSamples);
return color;
}
// -------------------------------------------------------------
// Techniques
// -------------------------------------------------------------
technique10 GaussianBlur
{
pass HBlur
{
VertexShader = compile vs_4_0 VS(float2(1, 0));
PixelShader = compile ps_4_0 PS();
}
pass VBlur
{
VertexShader = compile vs_4_0 VS(float2(0, 1));
PixelShader = compile ps_4_0 PS();
}
}