Generates sample offsets and weights for a two-pass Gaussian blur GLSL shader that uses linear texture filtering to sample two weighted pixels using a single texture read.

How to use it?

OFFSETS are offsets in pixels from the destination pixel to the input sample pixels (along the current blurring axis, i.e. horizontal or vertical).

WEIGHTS are the corresponding weights, i.e. how much contribution each input sample gives to the output value. They are already normalized — their sum is 1.

Here’s an example GLSL function that does the blurring:

How does it work?

A two-dimentional Gaussian filter uses weights in the form of , sampling the input texture in a square (in the range around the current pixel), making a total of texture reads in a single fragment shader invocation.

A separable filter makes use of the observation that , which means that we can blur horizontally over the range around the current pixel, and then blur the result vertically to get the final blur (or blur vertically first and horizontally after that, doesn’t matter). This cuts down the number of texture reads to per pass, meaning in total (for both the horizontal and vertical passes).

Using linear filtering for the input texture, we can further reduce the number of required texture reads. Say, we want to to read two neighbouring pixels p[i] and p[i+1] (I’m using 1D indexing because we’re talking about separable blur, so all pixels read in a single shader invocation are in the same row or column) with weights and . The total contribution of these two pixels is . Rewriting it as a lerp, we get , meaning we can sample at location with a total weight of , and thanks to linear filtering this will evaluate to the total contribution of two pixels, at the expense of a single texture read. This lowers the number of texture reads to per pass, meaning a total of per the full blur.

To learn about small sigma correction, see this post by Bart Wronski.

See also Alan Wolfe’s generator which uses a support instead of radius to figure out how many samples are needed.