{
 "id": "ordered-dithering-bayer",
 "title": "Ordered dithering with Bayer matrices",
 "aliases": [
  "Bayer dithering"
 ],
 "domain": "npr",
 "subdomain": "dithering",
 "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",
 "summary": "Quantizes each pixel against a threshold taken from a tiled Bayer matrix, producing a crisp, regular dither pattern at very low cost.",
 "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": "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` for less patterned, more natural-looking results, and `error-diffusion-dithering` for best static quality where temporal stability does not matter.",
 "cost": {
  "class": "cheap",
  "notes": "Trivial: one matrix lookup and a comparison per pixel."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://en.wikipedia.org/wiki/Ordered_dithering",
   "type": "web",
   "title": "Ordered dithering",
   "authors": [
    "Wikipedia"
   ],
   "year": null,
   "venue": "en.wikipedia.org",
   "url": "https://en.wikipedia.org/wiki/Ordered_dithering",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261118082634/https://en.wikipedia.org/wiki/Ordered_dithering",
     "timestamp": "20261118082634"
    },
    "page_title": "Ordered dithering - Wikipedia",
    "host": "en.wikipedia.org",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Ordered_dithering → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Ordered_dithering → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://en.wikipedia.org/wiki/Ordered_dithering → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0.95,
   "__bib": "wikipediaordered"
  }
 ],
 "presets": [
  {
   "id": "bayer-4x4-1bit",
   "title": "4x4 Bayer 1-bit dither of a lit sphere",
   "runtime": "glsl-fragment",
   "params": {
    "levels": 2,
    "cell_px": 3
   },
   "code": "// GLSL ES 3.0, Shadertoy-style: 4x4 Bayer ordered dithering of a procedural gradient\nconst float LEVELS = 2.0;   // output levels per channel (2 = pure 1-bit look)\nconst float PIXEL  = 3.0;   // size of one dither cell in screen pixels\nconst vec3  INK    = vec3(0.07, 0.06, 0.12);\nconst vec3  PAPER  = vec3(0.93, 0.90, 0.78);\nconst int BAYER4[16] = int[16](0,8,2,10, 12,4,14,6, 3,11,1,9, 15,7,13,5);\n\nfloat bayer4(ivec2 p){\n  return (float(BAYER4[(p.y & 3)*4 + (p.x & 3)]) + 0.5) / 16.0;\n}\nfloat scene(vec2 uv){\n  vec2 c = uv - vec2(0.0, 0.02);\n  float r = length(c);\n  float sphere = 0.0;\n  if(r < 0.32){\n    vec3 n = vec3(c, sqrt(0.32*0.32 - r*r)) / 0.32;\n    vec3 l = normalize(vec3(-0.5 + 0.2*sin(iTime), 0.6, 0.7));\n    sphere = clamp(dot(n, l), 0.0, 1.0);\n  }\n  float bg = 0.15 + 0.5*smoothstep(-0.5, 0.5, uv.x + 0.4*uv.y);\n  return mix(bg, 0.05 + 0.95*sphere, step(r, 0.32));\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord){\n  vec2 uv = (fragCoord - 0.5*iResolution.xy) / iResolution.y;\n  ivec2 cell = ivec2(floor(fragCoord / PIXEL));\n  vec2 uvq = (vec2(cell)*PIXEL + 0.5*PIXEL - 0.5*iResolution.xy) / iResolution.y;\n  float lum = scene(uvq);\n  float th = bayer4(cell);\n  float q = floor(lum*(LEVELS-1.0) + th) / (LEVELS-1.0);\n  fragColor = vec4(mix(INK, PAPER, clamp(q, 0.0, 1.0)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "ordered-dithering-bayer",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4314,
      "std": 0.4134,
      "unique_colors": 2
     },
     "image": "/specimens/bayer-4x4-1bit.webp",
     "reason": null
    }
   }
  },
  {
   "id": "ordered-dithering-bayer-wgsl",
   "title": "8×8 Bayer ordered dithering (WGSL)",
   "runtime": "webgpu-wgsl",
   "basis": "original",
   "license_note": "written for this index",
   "params": {
    "LEVELS": 3,
    "CELL_SIZE": 3
   },
   "code": "// WebGPU WGSL fragment specimen: recursive 8x8 Bayer threshold matrix\nconst LEVELS: f32 = 3.0000; // P\nconst CELL_SIZE: f32 = 3.0000; // P\n\nfn bayer2(a: vec2f) -> f32 {\n  let f = floor(a);\n  return fract(dot(f, vec2f(0.5, f.y * 0.75)));\n}\n\nfn bayer8(p: vec2f) -> f32 {\n  let f = floor(p) - 8.0 * floor(floor(p) / 8.0);\n  let b2 = bayer2(f);\n  let b4 = bayer2(floor(f * 0.5));\n  let b8 = bayer2(floor(f * 0.25));\n  return (b8 * 0.25 + b4) * 0.25 + b2;\n}\n\n@fragment\nfn fs_main(@builtin(position) pos: vec4f) -> @location(0) vec4f {\n  let cell = max(1.0, floor(CELL_SIZE + 0.5));\n  let p = floor(pos.xy / cell) * cell + 0.5 * cell;\n  let uv = (p - 0.5 * u.resolution) / u.resolution.y;\n  let d = length(uv);\n  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));\n  if (d < 0.36) {\n    let n = normalize(vec3f(uv.x, -uv.y, sqrt(max(0.0, 0.36 * 0.36 - d * d))));\n    let l = normalize(vec3f(cos(u.time * 0.5) * 0.7, 0.65, 0.6));\n    let shade = max(dot(n, l), 0.0) * 0.85 + 0.12;\n    src = vec3f(shade * 0.96, shade * 0.86, shade * 0.72);\n  }\n  let thr = bayer8(pos.xy / cell) - 0.5;\n  let lv = max(1.0, floor(LEVELS + 0.5));\n  let q = clamp(floor(src * lv + 0.5 + thr) / lv, vec3f(0.0), vec3f(1.0));\n  return vec4f(q, 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "wgsl",
    "label": "WebGPU WGSL runtime",
    "ok": true,
    "proof": null
   }
  }
 ],
 "related": [
  "blue-noise-dithering",
  "error-diffusion-dithering",
  "obra-dinn-1bit-dithered-3d",
  "pixel-art-3d-rendering"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "oklab-palette-quantization",
   "why": "Bayer threshold applied before nearest-palette lookup in a perceptual space gives clean palette dithering."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/npr/ordered-dithering-bayer.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Wikipedia",
 "url": "https://texelatlas.vercel.app/t/ordered-dithering-bayer/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 1.32,
  "tier": "feather",
  "p50_ms": 0.0433,
  "p95_ms": 0.0438,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "bayer-4x4-1bit",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 1.03,
   "tier": "feather",
   "p50_ms": 0.0163,
   "p95_ms": 0.0167,
   "speedup_x": 2.66,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 1,
   "tier": "feather",
   "p50_ms": 0.0288,
   "p95_ms": 0.0288,
   "speedup_x": 1.5,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}