← Bistro in cubes

The AgX curve

How to show, on a screen that has only 256 levels, a street where the sun on a window pane is thousands of times brighter than a wall in the shade.

The problem: the street has far more contrast than the screen

The renderer computes light as it is in the scene: one number per colour, with no limit. A wall in the shade receives little, a sky a lot, a reflection of the sun an enormous amount. In a sunlit street, the sun's reflection on a window pane can be about 65,000 times brighter than a deep shadow. Photographers count these gaps in stops: one stop is twice as much light. Doubling 16 times in a row gives 65,536: so the gap is about 16 stops. This is the unit on the ruler of the diagrams below.

Your screen, on the other hand, can only light each colour at 256 levels, from 0 (black) to 255 (the strongest white it has). So you need a rule that says, for each light in the scene, which level the screen lights. This is called a tone curve. AgX is one of them.

range of light in a sunlit street, about
1 to 65,000
levels per colour on the screen
256
the scene's middle grey is displayed at
118 / 255

Two simple rules that fail, and AgX

Here is the same scale of light, from the darkest on the left to the strongest on the right, each stop doubling the light. It is shown in three ways:

As is Scaled under the strongest With AgX wall in shadecobbles in sunskysun's reflection more light to the right: each stop of the scale doubles the light
These strips are computed with Blender's real table. As is: the light is sent to the screen unchanged; everything above the screen's white becomes the same flat white, and the sky and the reflection cannot be told apart. Scaled under the strongest: everything is divided so that the reflection fits; the rest becomes almost black. With AgX: the shadows and the midtones keep their nuances, and the strong lights are gently packed towards white, without being crushed.

The curve itself

Plotted on a chart, the “as is” rule rises and then stops dead at 255, as early as 2.5 stops above middle grey: everything brighter is lost. AgX follows the same line in the midtones, then bends: each extra stop rises less and less, and white is only reached beyond 6 stops. The sky and the reflection therefore stay distinct. The “scaled under the strongest” rule does the opposite: nothing is ever blocked, but everything is squashed down near the floor. The sunlit cobblestones fall to 3 out of 255, the sky to 22, and even the sun's reflection only rises to 99.

064128192255-6-4-20+2+4+6+8+10wall in shade: 35cobbles in sun: 118sky: 208sun's reflection: 255level on screenlight of the scene, in stops above or below middle grey (0)as isBlender's AgXscaled under the strongest
Blue dots: where a few things in the street fall with AgX, and their level on the screen; red dots: the same things displayed as is; orange dots: scaled under the strongest. The positions of the examples are orders of magnitude.

Colours fade as they do in a photo

An orange lantern, stronger and stronger. Displayed as is, its three colours block one after the other: red first, then green, so the orange turns bright yellow, then jumps to white all at once. With AgX, it stays orange while fading gently towards white, like a lamp that is too bright in a photo.

As is With AgX faint lantern lantern 2,000 times stronger
From left to right, the light doubles at each square. The colours are the ones Blender's table gives.

Why Blender's, and why a table

Every piece of software has its own variant. The Bevy engine, on which the client is built, has its own AgX, but it brightens the midtones by about a fifth compared with Blender's. Yet we compare the client's image with the one from Cycles, which Blender shows with its AgX. So we need exactly Blender's.

Rather than recode it and risk small differences, we asked Blender itself, once: “for each of these colours, what do you show on the screen?”. We took 64 levels of red, 64 of green and 64 of blue, from 11 stops below middle grey to 12 above: 64 × 64 × 64 = 262,144 colours. Blender's answers are stored in a 2 MB file.

scene colour (r, g, b) the table: 64 × 64 × 64 cells screen colour
At every frame, for every pixel, the client looks up the cell of its colour in the table and reads the answer, blending the neighbouring cells when the colour falls between two. It is a table read on the graphics card, very fast. Blender itself is no longer running.

Checked against images that Blender displays itself, this table gives the same result to within 53.9 dB, that is, with no visible difference. It is produced by tools/bistro/agx-lut.py and stored in assets/blender/agx-64.bin.