---
id: "auto-exposure-histogram"
title: "Auto exposure from luminance histogram"
aliases: [eye adaptation]
domain: "post"
stage: "post"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, mobile-gpu, unity, unreal, godot]
year_introduced: 2016
inputs: [hdr-image]
outputs: [hdr-image]
style: "photoreal"
dynamic: "dynamic"
cost: "cheap"
authority: "Opsenica"
url: "https://texelatlas.vercel.app/t/auto-exposure-histogram/"
sources_verified_at: "2026-11-29"
---
# Auto exposure from luminance histogram (Opsenica)

> Builds a luminance histogram of the HDR frame in a compute pass, discards outliers, averages the rest in log space, and smoothly adapts exposure over time, giving a stable eye-adaptation response.

## Reach for it when

- scene is too dark then blown out when moving between rooms
- need eye adaptation that ignores tiny bright lights
- exposure pumps and flickers when a bright pixel enters view
- want smooth automatic brightness adjustment over time
- average luminance gets skewed by outliers

## How it works

A compute shader bins every pixel's log2 luminance into a histogram of around 256 buckets, with a small weight toward the screen centre if desired. A second dispatch walks the histogram, trims the darkest and brightest percentiles so tiny lights and black borders do not skew the result, and computes the weighted mean log luminance. That mean maps to a target exposure through an exposure compensation and key value, for example mapping the mean to mid grey. The scene exposure is blended toward the target with an exponential filter, using different rates for brightening and darkening, which mimics pupil adaptation. The exposure is then applied before the tonemapper.

## When to use it

Choose it when scene brightness varies a lot and you want eye-adaptation with outlier rejection.

Prefer a simple log-average mip chain when compute is unavailable, as in WebGL2. Choose fixed exposure for photographic or art-directed shots. Feed the result into the tonemappers, for example [AgX](https://texelatlas.vercel.app/t/agx-tonemapping.md).

## Cost

cheap — One histogram compute pass and one tiny reduction; cost dominated by memory bandwidth of reading the frame.
Measured GPU Weight: 1.2× baseline (feather) — 0.0396 ms median / 0.0409 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 1.09× baseline (feather) — 0.0174 ms median (2.28× faster on L4).
Ampere A100 scaling: 21.24× baseline (heavy) — 0.6118 ms median (0.06× faster on A100).

## Failure modes

- Pumping or oscillation if the adaptation rate is too fast or the metering follows moving lights
- Wrong exposure when the frame contains huge bright skies or pure black letterboxing without percentile trimming
- Temporal lag causes overexposed frames on cuts unless exposure is reset
- Needs compute shaders, so WebGL2 requires a fallback

## Engine support

- godot: builtin — `CameraAttributes.auto_exposure_enabled (exposure adaptation from scene luminance)` (evidence: https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/CameraAttributes.xml, checked 2026-12-01)
- unity: builtin — `Volume > Add Override > Exposure > Mode = Automatic Histogram` (evidence: https://github.com/Unity-Technologies/Graphics/blob/557f6ac22b948284f57ffae6a789751fd1f63620/Packages/com.unity.render-pipelines.high-definition/Documentation~/reference-override-exposure.md, checked 2026-12-01) — HDRP only. URP has no automatic exposure in Unity 6.3.
- unreal: builtin — `Post Process Volume > Exposure > Metering Mode = Auto Exposure Histogram` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/auto-exposure-in-unreal-engine, checked 2026-12-01)

## Dataflow

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

## Presets

### Dim room with a bright window. Left: fixed exposure. Right: exposure from a luminance histogram with 10% tails discarded (glsl-fragment; rendered 2026-11-29)

```json
{
  "GN": 16,
  "NB": 24,
  "LO": 0.1,
  "HI": 0.9
}
```

```glsl
float hash(vec2 c){uvec2 u=uvec2(ivec2(floor(c)));uint h=u.x*1597334673u^u.y*3812015801u;h^=h>>15;h*=2246822519u;h^=h>>13;h*=3266489917u;h^=h>>16;return float(h)*(1./4294967296.);}
float lum(vec3 c){return dot(c,vec3(.2126,.7152,.0722));}
vec3 gam(vec3 c){return pow(clamp(c,0.,1.),vec3(1./2.2));}

const int GN=16; // P
const int NB=24; // P
const float LO=0.1; // P
const float HI=0.9; // P
vec3 hdr(vec2 p){
  vec3 c=vec3(.02,.025,.03)*(1.+(p.y+.5)*.8);
  if(p.y<-.3)c=vec3(.05,.035,.025)*(.6+.8*step(.5,fract((p.x+p.y)*12.)));
  if(length(p-vec2(-.1,-.1))<.09)c=vec3(.5,.2,.1)*.12;
  if(abs(p.x-.08)<.1&&p.y>.1&&p.y<.4)c=vec3(1.,.95,.85)*18.;
  return c;}
void mainImage(out vec4 fragColor,in vec2 fragCoord){
  vec2 uv=(fragCoord-.5*iResolution.xy)/iResolution.y;bool R=uv.x>0.;vec2 q=vec2(uv.x+(R?-.25:.25),uv.y);
  float cnt[24];for(int b=0;b<NB;b++)cnt[b]=0.;
  for(int j=0;j<GN;j++)for(int i=0;i<GN;i++){
    vec2 p=vec2((float(i)+.5)/float(GN)-.5,(float(j)+.5)/float(GN)-.5)*vec2(.5,1.);
    float lg=log2(max(lum(hdr(p)),1e-4));int b=int(clamp(floor((lg+8.)/14.*float(NB)),0.,float(NB-1)));cnt[b]+=1.;}
  float tot=float(GN*GN),acc=0.,sum=0.,ws=0.,mx=0.;float kept[24];
  for(int b=0;b<NB;b++){float a1=acc+cnt[b];float k=max(min(a1,HI*tot)-max(acc,LO*tot),0.);
    kept[b]=k>0.?1.:0.;sum+=k*(-8.+(float(b)+.5)/float(NB)*14.);ws+=k;acc=a1;mx=max(mx,cnt[b]);}
  float expo=.18/exp2(sum/max(ws,1e-3));
  vec3 c=hdr(q)*(R?expo:1.);
  vec3 col=gam(c/(1.+c));
  if(R&&uv.y<-.3&&uv.x>.03&&uv.x<.47){
    int b=int(floor((uv.x-.03)/.44*float(NB)));float h=cnt[b]/mx*.17;
    col*=.35;
    if(uv.y+.5<h) col=kept[b]>.5?vec3(.3,.9,.5):vec3(.6,.3,.3); }
  if(abs(uv.x)<.0015)col=vec3(1.);
  fragColor=vec4(col,1.);
}
```

Attribution: written for this index

## Combines with

- [ACES filmic tonemapping](https://texelatlas.vercel.app/t/aces-tonemapping.md): Exposure scales scene-referred values before the tone curve.

## Combination ideas

- [1-bit rendering that meters like a camera](https://texelatlas.vercel.app/ideas/histogram-steered-dithering/)

## Sources

1. [primary] Bruno Opsenica Automatic Exposure. bruop.github.io. https://bruop.github.io/exposure (verified via http on 2026-11-29)
2. [implementation] EmbarkStudios (2021) EmbarkStudios/kajiya. https://github.com/EmbarkStudios/kajiya License: Apache-2.0. (verified via github on 2026-11-29)

## Cite this entry

Texel. "Auto exposure from luminance histogram." https://texelatlas.vercel.app/t/auto-exposure-histogram/ (accessed 2026-12-01).
