• Create Account

## Problem with bluring using compute shader

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

1 reply to this topic

### #1BlackBrain  Members

509
Like
0Likes
Like

Posted 15 August 2014 - 05:55 PM

Hello.

I have implemented Gaussian Blur using compute shaders and it works good but as I increase the alpha the outcome becomes darker and darker. And I don't know understand why because the weights sum is equal to one.

This is how I calculate weights :

void CalculateWeights()
{
weights = new float[2 * BlurRadius+1];

for (int i = 0; i < weights.Length; i++)
{
uint x = (uint)i-BlurRadius;
weights[i] = (float)Math.Exp(- (  (x*x)/(2*Alpha*Alpha)  )  ) ;
}

float sum = 0;

foreach (float weight in weights)
{
sum += weight;
}

for (int i = 0; i < weights.Length; i++)
{
weights[i] /= sum;
}

}


And this is Horizontal Blur shader:

cbuffer Globals
{
float Width;
float Height;
};

Texture2D Input;
RWTexture2D<float4> Output;

SamplerState ClampSampler
{
Filter = MIN_MAG_MIP_LINEAR;

};

{

//set left pixels
{
float2 SamplePos = float2((dispathThreadId.x - BlurRadius) / Width, dispathThreadId.y / Height);
Cache[groupThreadId.x] = Input.SampleLevel(ClampSampler, SamplePos,0);
}

//set right pixels
{
float2 SamplePos = float2((dispathThreadId.x + BlurRadius) / Width, dispathThreadId.y / Height);
Cache[groupThreadId.x + 2 * BlurRadius] = Input.SampleLevel(ClampSampler, SamplePos,0);
}

float2 SamplePos = float2(dispathThreadId.x / Width, dispathThreadId.y / Height);

// Wait for all threads to finish sampling
GroupMemoryBarrierWithGroupSync();

float4 blurColor = float4(0,0,0,0);

[unroll]
for (int i = -BlurRadius; i < BlurRadius; i++)
{
}

Output[dispathThreadId.xy] = float4(blurColor.xyz, 1.0f);
}

technique11 Tech0
{
pass P0
{
}
}


And the Vertical Blur Shader:

cbuffer Globals
{
float Width;
float Height;
};

Texture2D Input;
RWTexture2D<float4> Output;

SamplerState ClampSampler
{
Filter = MIN_MAG_MIP_LINEAR;

};

{

//set up pixels
{
float2 SamplePos = float2(dispathThreadId.x / Width, (dispathThreadId.y - BlurRadius) / Height);
Cache[groupThreadId.y] = Input.SampleLevel(ClampSampler, SamplePos,0);
}

//set down pixels
{
float2 SamplePos = float2(dispathThreadId.x / Width, (dispathThreadId.y + BlurRadius) / Height);
Cache[groupThreadId.y + 2 * BlurRadius] = Input.SampleLevel(ClampSampler, SamplePos,0);
}

float2 SamplePos = float2(dispathThreadId.x / Width, dispathThreadId.y / Height);

// Wait for all threads to finish sampling
GroupMemoryBarrierWithGroupSync();

float4 blurColor = float4(0,0,0,0);

[unroll]
for (int i = -BlurRadius; i < BlurRadius; i++)
{
}

Output[dispathThreadId.xy] = float4(blurColor.xyz, 1.0f);
}

technique11 Tech0
{
pass P0
{
}
}


Any idea why is this happening? The darkening is more clear when Blur radius is a low number such as 2 or 3.

### #2BlackBrain  Members

509
Like
1Likes
Like

Posted 16 August 2014 - 12:37 PM

I figured it out myself. the for loops shoud be as follows :

for (int i = -BlurRadius; i <= BlurRadius; i++)
{
}

It needed the i<=BlurRadius

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.