---
id: "ordered-dithering-bayer"
title: "Ordered dithering with Bayer matrices"
aliases: [Bayer dithering]
domain: "npr"
stage: "post"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot, cpu]
year_introduced: 1973
inputs: [ldr-image, hdr-image]
outputs: [ldr-image]
style: "stylized"
dynamic: "both"
cost: "cheap"
authority: "Wikipedia"
url: "https://texelatlas.vercel.app/t/ordered-dithering-bayer/"
sources_verified_at: "2026-11-29"
---
# Ordered dithering with Bayer matrices (Wikipedia)

> Quantizes each pixel against a threshold taken from a tiled Bayer matrix, producing a crisp, regular dither pattern at very low cost.

## Reach for it when

- need retro dithered look with cheap per-pixel cost
- banding when reducing colour depth
- dither pattern must not shimmer between frames
- limited palette output with stable pattern
- 1-bit or low-colour image conversion in a shader

## How it works

A Bayer matrix of size 2^n is a recursively built grid of threshold ranks (for 4×4: values 0 to 15). For each pixel, look up the threshold at (x mod n, y mod n), normalise it to (0,1), add it to the scaled input luminance, and floor to the target number of levels. For a 1-bit result this is simply `lum > threshold`. The recursion guarantees that the dots turn on in a spread-out, balanced order as brightness rises, so the pattern shows as the familiar crosshatch-grid texture. Per-channel dithering with limited palettes works the same way. Applying it at a lower virtual resolution than the screen, with nearest-neighbour upscaling, gives the retro look.

## When to use it

Choose it when you want a deterministic, stable, retro pattern that does not shimmer with camera motion and is trivially cheap. Prefer [Blue-noise dithering](https://texelatlas.vercel.app/t/blue-noise-dithering.md) for less patterned, more natural-looking results, and [Error-diffusion dithering (Floyd-Steinberg)](https://texelatlas.vercel.app/t/error-diffusion-dithering.md) for best static quality where temporal stability does not matter.

## Cost

cheap — Trivial: one matrix lookup and a comparison per pixel.
Measured GPU Weight: 1.32× baseline (feather) — 0.0433 ms median / 0.0438 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 1.03× baseline (feather) — 0.0163 ms median (2.66× faster on L4).
Ampere A100 scaling: 1× baseline (feather) — 0.0288 ms median (1.5× faster on A100).

## Failure modes

- Visible regular crosshatch pattern at low level counts
- Pattern is screen-fixed, so it appears to slide over moving 3D objects (shower door)
- Banding on smooth gradients at very small matrix sizes
- Colour dithering with unequal per-channel thresholds shows hue shifts

## Engine support

- threejs: official — `import { bayerDither } from 'three/addons/tsl/math/Bayer.js'` (evidence: https://github.com/mrdoob/three.js/blob/r187/examples/jsm/tsl/math/Bayer.js, checked 2026-12-01)

## Dataflow

- Inputs: ldr-image, hdr-image
- Outputs: ldr-image
- Platforms: webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot, cpu

## Presets

### 4x4 Bayer 1-bit dither of a lit sphere (glsl-fragment; rendered 2026-11-29)

```json
{
  "levels": 2,
  "cell_px": 3
}
```

```glsl
// GLSL ES 3.0, Shadertoy-style: 4x4 Bayer ordered dithering of a procedural gradient
const float LEVELS = 2.0;   // output levels per channel (2 = pure 1-bit look)
const float PIXEL  = 3.0;   // size of one dither cell in screen pixels
const vec3  INK    = vec3(0.07, 0.06, 0.12);
const vec3  PAPER  = vec3(0.93, 0.90, 0.78);
const int BAYER4[16] = int[16](0,8,2,10, 12,4,14,6, 3,11,1,9, 15,7,13,5);

float bayer4(ivec2 p){
  return (float(BAYER4[(p.y & 3)*4 + (p.x & 3)]) + 0.5) / 16.0;
}
float scene(vec2 uv){
  vec2 c = uv - vec2(0.0, 0.02);
  float r = length(c);
  float sphere = 0.0;
  if(r < 0.32){
    vec3 n = vec3(c, sqrt(0.32*0.32 - r*r)) / 0.32;
    vec3 l = normalize(vec3(-0.5 + 0.2*sin(iTime), 0.6, 0.7));
    sphere = clamp(dot(n, l), 0.0, 1.0);
  }
  float bg = 0.15 + 0.5*smoothstep(-0.5, 0.5, uv.x + 0.4*uv.y);
  return mix(bg, 0.05 + 0.95*sphere, step(r, 0.32));
}
void mainImage(out vec4 fragColor, in vec2 fragCoord){
  vec2 uv = (fragCoord - 0.5*iResolution.xy) / iResolution.y;
  ivec2 cell = ivec2(floor(fragCoord / PIXEL));
  vec2 uvq = (vec2(cell)*PIXEL + 0.5*PIXEL - 0.5*iResolution.xy) / iResolution.y;
  float lum = scene(uvq);
  float th = bayer4(cell);
  float q = floor(lum*(LEVELS-1.0) + th) / (LEVELS-1.0);
  fragColor = vec4(mix(INK, PAPER, clamp(q, 0.0, 1.0)), 1.0);
}

```

Attribution: written for this index

### 8×8 Bayer ordered dithering (WGSL) (webgpu-wgsl; WebGPU WGSL runtime)

```json
{
  "LEVELS": 3,
  "CELL_SIZE": 3
}
```

```wgsl
// WebGPU WGSL fragment specimen: recursive 8x8 Bayer threshold matrix
const LEVELS: f32 = 3.0000; // P
const CELL_SIZE: f32 = 3.0000; // P

fn bayer2(a: vec2f) -> f32 {
  let f = floor(a);
  return fract(dot(f, vec2f(0.5, f.y * 0.75)));
}

fn bayer8(p: vec2f) -> f32 {
  let f = floor(p) - 8.0 * floor(floor(p) / 8.0);
  let b2 = bayer2(f);
  let b4 = bayer2(floor(f * 0.5));
  let b8 = bayer2(floor(f * 0.25));
  return (b8 * 0.25 + b4) * 0.25 + b2;
}

@fragment
fn fs_main(@builtin(position) pos: vec4f) -> @location(0) vec4f {
  let cell = max(1.0, floor(CELL_SIZE + 0.5));
  let p = floor(pos.xy / cell) * cell + 0.5 * cell;
  let uv = (p - 0.5 * u.resolution) / u.resolution.y;
  let d = length(uv);
  var src = mix(vec3f(0.95, 0.94, 0.90), vec3f(0.14, 0.17, 0.22), clamp(uv.x + 0.55, 0.0, 1.0));
  if (d < 0.36) {
    let n = normalize(vec3f(uv.x, -uv.y, sqrt(max(0.0, 0.36 * 0.36 - d * d))));
    let l = normalize(vec3f(cos(u.time * 0.5) * 0.7, 0.65, 0.6));
    let shade = max(dot(n, l), 0.0) * 0.85 + 0.12;
    src = vec3f(shade * 0.96, shade * 0.86, shade * 0.72);
  }
  let thr = bayer8(pos.xy / cell) - 0.5;
  let lv = max(1.0, floor(LEVELS + 0.5));
  let q = clamp(floor(src * lv + 0.5 + thr) / lv, vec3f(0.0), vec3f(1.0));
  return vec4f(q, 1.0);
}
```

Attribution: written for this index

## Lineage

- extended by [Blue-noise dithering](https://texelatlas.vercel.app/t/blue-noise-dithering.md)
- extended by [1-bit dithered 3D rendering (Obra Dinn style)](https://texelatlas.vercel.app/t/obra-dinn-1bit-dithered-3d.md)

## Combines with

- [OKLab palette quantization](https://texelatlas.vercel.app/t/oklab-palette-quantization.md): Bayer threshold applied before nearest-palette lookup in a perceptual space gives clean palette dithering.

## Sources

1. [primary] Wikipedia Ordered dithering. en.wikipedia.org. https://en.wikipedia.org/wiki/Ordered_dithering (verified via http on 2026-11-29)

## Cite this entry

Texel. "Ordered dithering with Bayer matrices." https://texelatlas.vercel.app/t/ordered-dithering-bayer/ (accessed 2026-12-01).
