Gaussian Kernel Generator
This page was made to generate a reasonably-ideal 1D gaussian filter for a given kernel size.
Additionally, it generates optimized coefficients for linear filtered texture sampling.
Kernel size:
Coefficient Graph
Generated sigma (σ) value:
Standard 1D Coefficients
Optimized Coefficients For Bilinear Filtering
This is a set of coefficients and offsets for use with linear texture sampling, to do less actual texture
samples (roughly half) by taking advantage of the linear filtering.
- The coefficients list is how much to scale each texture sample.
- The offsets list is a list of offsets from the center of the filter (in texels).
Coefficients
Offsets
Sample HLSL Code
Below is some (untested) sample code that uses the optimized coefficients and offsets computed above
to blur an image in a given direction.
Texture2D<unorm float4> g_sourceTex;
// This must be set up for bilinear filtering for this to work
sampler g_sampler;
static const uint k_sampleCount =
/* number of optimized coefficients */;
static const float k_coeffs[k_sampleCount] =
{ /* optimized coefficients calcualted above */ };
static const float k_offsets[k_sampleCount] =
{ /* offsets calculated above */ };
// Blur a texture along the blur direction (for a horizontal blur, use
// [1, 0] and for vertical use [0, 1]), centered at "centerTexCoord"
// (which is in standard [0..1] texture space).
float4 Blur(float2 centerTexCoord, float2 blurDirection)
{
uint2 dim;
g_sourceTex.GetDimensions(dim.x, dim.y);
float4 v = float4(0, 0, 0, 0);
for (uint i = 0; i < k_sampleCount; i++)
{
float2 c = centerTexCoord
+ blurDirection / float2(dim) * k_offsets[i];
v += g_sourceTex.Sample(g_sampler, c) * k_coeffs[i];
}
return v;
}
// This must be set up for bilinear filtering for this to work
sampler g_sampler;
static const uint k_sampleCount =
/* number of optimized coefficients */;
static const float k_coeffs[k_sampleCount] =
{ /* optimized coefficients calcualted above */ };
static const float k_offsets[k_sampleCount] =
{ /* offsets calculated above */ };
// Blur a texture along the blur direction (for a horizontal blur, use
// [1, 0] and for vertical use [0, 1]), centered at "centerTexCoord"
// (which is in standard [0..1] texture space).
float4 Blur(float2 centerTexCoord, float2 blurDirection)
{
uint2 dim;
g_sourceTex.GetDimensions(dim.x, dim.y);
float4 v = float4(0, 0, 0, 0);
for (uint i = 0; i < k_sampleCount; i++)
{
float2 c = centerTexCoord
+ blurDirection / float2(dim) * k_offsets[i];
v += g_sourceTex.Sample(g_sampler, c) * k_coeffs[i];
}
return v;
}