Introduction#
Render targets let a fragment shader read a whole finished image. That opens up image processing: effects that change each pixel based on the pixels around it. The most fundamental of these is convolution, and once you understand it, blurring, sharpening, finding edges, glows around bright lights and camera-style focus blur all fall into place. This post covers:
- Convolution kernels: a small grid of weights that defines an effect
- The classic kernels: blur, sharpen, edge detection and emboss
- The Gaussian blur, the workhorse of post-processing
- Separable filters: how splitting a blur into two passes makes it dramatically cheaper
Kernels: A Weighted Sum of Neighbours#
A kernel is a small grid of numbers, for example 3 × 3. To apply it to a pixel, place the kernel’s centre over that pixel, multiply each of the nine pixels underneath by the weight on top of it, and add them all up. Then divide by the total of the weights, so the overall brightness stays the same. (If the weights add up to zero, as for edge detection below, there’s nothing to divide by: the sum is used as it is.) Do that for every pixel and you’ve convolved the image with the kernel.
In a fragment shader, that’s a short piece of code that repeats nine times (a loop), reading the nine texels around the current one. Edit the weights below, or pick a preset. Sample spacing spreads the nine samples further apart, which exaggerates the effect so it’s easier to see on a high-resolution screen.
Applying a 3 × 3 convolution kernel to an image
What each preset does:
- Box blur gives all nine pixels the same weight: each pixel becomes the average of its neighbourhood, smoothing out detail. Watch the fine stripes turn grey.
- Gaussian blur weights the centre most and the corners least, which blurs more smoothly (more on this below).
- Sharpen gives the centre a big positive weight and the neighbours negative ones. Where a pixel differs from its neighbours, the difference is exaggerated, so edges get crisper.
- Edge detection has weights that add up to zero. In a flat area, the centre’s +8 cancels the eight neighbours’ −1s exactly, giving black; only where brightness changes does anything survive. This kind of filter is used for outlines and cartoon effects.
- Emboss is lopsided: it brightens edges on one side and darkens them on the other, which looks like light hitting raised shapes.
Try your own: a single 1 in a corner and zeros elsewhere just shifts the image by one sample.
The Gaussian Blur#
A box blur has a problem: every pixel inside the box counts equally and every pixel outside counts not at all, so a bright point blurs into a visible square. A Gaussian blur uses weights that follow the bell-shaped Gaussian curve, strongest in the middle and fading smoothly to nothing. A bright point blurs into a soft, round glow, which looks natural, like an out-of-focus camera. It’s used everywhere in post-processing: for glows around bright lights, out-of-focus backgrounds, frosted glass, soft shadows and blurring the background behind menus.
A nice blur needs a large kernel, though, not 3 × 3. A radius of 10 pixels means a 21 × 21 kernel: 441 texture reads for every single pixel. At 1920 × 1080, that’s over 900 million reads for one blur.
Separable Filters: Two Passes Instead of One#
The Gaussian has a beautiful property: blurring horizontally and then blurring the result vertically gives exactly the same result as one big 2D blur. A filter that can be split like this is called separable. So instead of one pass of 21 × 21 = 441 reads, you do one pass of 21 reads across, into a render target, then a second pass of 21 reads down: 42 reads per pixel, more than ten times fewer, for an identical image. The bigger the blur, the bigger the saving.
A Gaussian blur done in one 2D pass, or as two 1D passes
Switch between the two methods at the same radius: the pictures are the same, but the number of reads isn’t. Choose Horizontal pass only to see the halfway point, where each row has been smeared sideways on its own. (The 2D method is capped at a radius of 24 here; much larger and it could make your browser stutter.)
The box blur is separable too, but many kernels aren’t: sharpen, edge detection and emboss above can’t be split into a horizontal and a vertical pass.
How the Gaussian weights are worked out
The weight for a texel x steps from the centre is:
Here e is a mathematical constant (about 2.718), and raising it to a negative power gives a number that starts at 1 when x = 0 and falls smoothly towards 0: the bell curve. σ (“sigma”) sets how wide the bell is; this demo uses σ = radius ÷ 2.5, so at the edge of the radius the weight has faded to about 4% of the centre weight. In 2D, the weight uses x² + y², the squared distance from the centre.
Finally each weight is divided by the total of all the weights, so they add up to 1 and the brightness doesn’t change. The separable trick works because e−(x² + y²) ÷ (2σ²) is exactly e−x² ÷ (2σ²) × e−y² ÷ (2σ²): every 2D weight is a horizontal weight times a vertical one.
Real blur shaders cut the cost even further with a trick using bilinear filtering (part 2 of Textures). If you sample exactly between two texels, the texture unit returns their average for the price of one read; sample at just the right point between them and it returns any weighted blend you like. So one carefully placed bilinear sample can do the work of two kernel weights, nearly halving the reads again. Large blurs are also usually done on downscaled copies of the image (half, quarter or smaller), because a blur removes fine detail anyway: blurring a quarter-resolution image by 5 pixels looks almost identical to blurring the full image by 20, at a tiny fraction of the cost.
Questions You Might Have#
What happens at the edges of the image, where some neighbours are missing?
Something has to be chosen. A common choice, used here, is to clamp: reading past the edge gives the nearest edge pixel again. Other choices are wrapping round to the opposite side or treating missing pixels as black.
Summary#
- Convolution replaces each pixel with a weighted sum of its neighbours; the weights form a kernel.
- Different kernels blur, sharpen, detect edges or emboss.
- The Gaussian blur looks natural, but large kernels are expensive.
- It’s separable: a horizontal pass then a vertical pass gives the same result for far fewer reads.
Next: HDR, tone mapping and bloom, where blur makes bright lights glow.
Further Reading#
- Victor Powell, Image Kernels Explained Visually: a lovely interactive explanation of kernels.
- Daniel Rákos, Efficient Gaussian blur with linear sampling: the bilinear trick in detail.
- LearnOpenGL, Framebuffers: Kernel effects: kernels as post-processing in OpenGL.
- Wikipedia, Kernel (image processing) and Gaussian blur: more kernels and the maths of separability.