GPU data
Color spaces: sRGB and linear
A color value can mean two different things: an amount of light, or a code chosen to look even to the eye. Images are stored as codes (sRGB). Light must be computed as amounts (linear). This page explains the sRGB curve, why the math must happen in linear, which textures are colors and which are data, and how the GPU converts between the two for free. The last part shows how RASM does it.
In short
- A linear value is proportional to the light: 0.5 is half as much light as 1.0.
- An sRGB value is a code on a curve that spends more of the 256 steps on dark tones, where eyes are sensitive. Linear 0.5 is sRGB 0.735 (byte 188); sRGB 0.5 is only 21% of the light.
- Lighting, blending, filtering and averaging must use linear values. Doing them on sRGB codes gives wrong, usually too dark, results.
- Color textures (base color, emissive) are sRGB. Data textures (normals, roughness, metalness, occlusion) are plain numbers and must not be converted.
- Formats ending in _SRGB make the GPU decode on read and encode on write, in hardware. The shader sees and writes linear values only.
- Light can be brighter than 1.0. Tone mapping squeezes it into 0 to 1 before the final encode.
1Two kinds of numbers
The same number can be read by physics or by the eye. The two do not agree.
Light adds up: linear
Light is made of photons. Twice the photons is twice the light, and two lamps together give the sum of both. A number that works like that is linear: it is proportional to the physical amount of light. All of physics, and so all lighting math, works on linear numbers.
Eyes notice ratios
Eyes do not work like that. Going from 1% to 2% of the light looks like a big step, going from 50% to 51% looks like nothing.
If an 8-bit image stored linear values, most of its 256 steps would be spent on bright tones that look the same, and the dark tones would show visible bands.
So images are stored as codes on a curve that roughly matches the eye. The curve used by almost every screen, photo and web page is sRGB. A program that wants to do math on light must first decode the code to a linear value, do the math, and encode the result for the screen.
- sRGB codewhat the image stores
- decodecode → linear value
- mathon amounts of light
- encodelinear value → code
- screenshows the code
2The sRGB curve
The sRGB standard (IEC 61966-2-1) defines the curve exactly. It is a power curve with a short straight piece near black, so that the slope at 0 is not infinite. Values are between 0 and 1 (divide a byte by 255).
decode (sRGB code s → linear light L):
L = s / 12.92 if s ≤ 0.04045
L = ((s + 0.055) / 1.055) ^ 2.4 otherwise
encode (linear light L → sRGB code s):
s = 12.92 · L if L ≤ 0.0031308
s = 1.055 · L ^ (1 / 2.4) − 0.055 otherwise
The value people find surprising: a pixel stored as byte 128, "half" of 255, shows only 21% of full light. And half of full light is byte 188.
Try any byte. The picture below runs the decode formula on it.
Values to remember
| Linear | sRGB code | Byte | |
|---|---|---|---|
| 0.0 | 0.000 | 0 | |
| 0.01 | 0.100 | 25 | |
| 0.18 | 0.461 | 118 | "middle gray" |
| 0.214 | 0.500 | 128 | |
| 0.5 | 0.735 | 188 | |
| 1.0 | 1.000 | 255 |
A plain power of 2.2 is a common shortcut: 0.5 ^ (1/2.2) = 0.730, close to 0.735. It differs most near black, where the real curve is a straight line.
3Why the math must be linear
Lighting is adding and scaling amounts of light: a surface under two lamps gets the sum of both, a surface turned away gets a fraction. That is only true for linear values. On sRGB codes, "half" and "plus" mean something else, and every result comes out wrong.
Follow one texel from the texture to the screen. The texture says the surface is gray, stored as byte 128. Two lamps each give it half of full light.
1. The texel: byte 128, an sRGB code
This is what the artist's image file holds: a code chosen so that it looks like a mid gray on screen.
2. Decode to linear
Now the number means something physical: the fraction of light the surface reflects (its albedo).
3. Light it: two lamps, half light each
Adding and scaling linear values is just physics. This is the part the shader does.
4. Encode for the screen
The screen's own curve then turns the code back into light. The round trip is exact.
Now the same with one lamp, done wrong and done right. One lamp must give half the light of two:
Wrong: math on codes
0.502 × 0.5 = 0.251, written as is. Byte on screen: 64. Light on screen: 5.1%. A quarter, not half.
Right: linear math
Decode 128 → 0.216, × 0.5 = 0.108, encode → 0.362. Byte on screen: 92. Light on screen: 10.8%, half of 21.6%.
Every shadow and every falloff is too dark. Light sources add up wrongly: two lamps that each give a mid gray do not add to the brightness one lamp twice as strong would give. The look is harsh, with sudden dark edges. Linear lighting gives soft, physically plausible falloff.
4Color textures and data textures
Only textures that hold colors meant to be seen are stored as sRGB. Many textures hold numbers: a direction, a roughness, a mask. Those are already linear, and decoding them as sRGB changes the numbers.
Colors: store as sRGB
Base color
Also called albedo or diffuse. Holds a color.
Store as sRGB
Emissive
Holds a color: the light the surface gives off.
Store as sRGB
UI images, sprites, photos
Hold colors.
Store as sRGB
Numbers: store as linear
Normal map
A direction: (x, y, z) mapped from −1…1 to 0…1.
Store as linear (UNORM)
Metallic-roughness
Two factors from 0 to 1.
Store as linear (UNORM)
Ambient occlusion
A fraction of light.
Store as linear (UNORM)
Height, masks, lookup tables
Hold numbers.
Store as linear (UNORM)
Two special cases
Alpha channel of any texture
Holds coverage.
Always linear, even in an sRGB format
HDR images, light maps
Hold amounts of light above 1.
Store as linear float (16-bit)
What happens when a normal map is loaded as sRGB by mistake:
5The GPU converts for free
Modern GPUs do the sRGB curve in hardware. The format of an image says whether its bytes are sRGB codes. A format ending in _SRGB (for example R8G8B8A8_SRGB) means: decode when read, encode when written. The same bytes in a _UNORM format are read as plain numbers divided by 255.
What the hardware does with an _SRGB format, step by step:
Sample
Decodes each texel it reads to linear, then filters (bilinear, trilinear) the linear values. Alpha is not decoded.
Write
Encodes the shader's linear output to sRGB codes as it stores them.
Blend
Reads the stored code, decodes it, blends in linear, encodes the result.
Copy, clear
A copy moves bytes unchanged. A clear color is given in linear and encoded.
Can the shader convert instead: read a UNORM texture and call pow? That works for a single read but is wrong with filtering. Take a sample halfway between a black texel and a white one:
UNORM format, pow in the shader
The GPU filters the codes, before the shader can decode them.
_SRGB format
The sampler decodes first, then filters. Use the _SRGB format and let the sampler do it.
6Filtering and blending are math too
Any operation that mixes colors must mix linear values. The same black-and-white example comes up again and again. Each picture mixes white and black 50/50:
Bilinear filtering
A sample between a black and a white texel.
On codes: 128 · In linear: 188
Alpha blending
White at 50% opacity over black.
On codes: 128 · In linear: 188
Anti-aliasing (MSAA resolve)
An edge pixel half covered.
On codes: 128 · In linear: 188
The same mix at any ratio, and with colors. Both bars below go from the first color to the second in small steps; only the space of the math differs.
On codes, every mix is too dark. Thin bright lines (text, wires, stars) look thinner and darker than they should, and edges look jagged even with anti-aliasing.
7Light above 1.0: tone mapping
Linear light has no upper limit. A lamp-lit white wall can be 4.0, a spot of sunlight 20.0. The screen's codes stop at 1.0. Simply clipping everything above 1 turns all bright areas into the same flat white. Tone mapping is a curve that squeezes the whole range into 0 to 1, keeping the order of brightness. It runs on linear values, before the sRGB encode.
Reinhard, then encode
| Light | Mapped | Byte | Byte if clipped |
|---|---|---|---|
| 0.5 | 0.333 | 156 | 188 |
| 1.0 | 0.500 | 188 | 255 |
| 2.0 | 0.667 | 213 | 255 |
| 4.0 | 0.800 | 231 | 255 |
| 8.0 | 0.889 | 242 | 255 |
Every brightness keeps its own value. The price: mid tones get darker and colors lose saturation.
To have values above 1 to map, the scene must be rendered into a target that can store them: a 16-bit float format, not an 8-bit one. A typical frame:
- Render the scenein linear float
- Exposure and tone mappingin one full-screen pass
- sRGB swapchainthe result is written to it
8Pitfalls
- Washed out, gray, low contrast
- Encode twice: the shader applies the curve, and the _SRGB target encodes again. Linear 0.5 → 0.735 → 0.873 (byte 223 instead of 188).
- Too dark, harsh shadows
- Never encode: linear output to a UNORM target. Linear 0.5 stored as 128, shown as 21% light.
- Bent normals, too-shiny surfaces
- Data texture loaded as sRGB. See section 4.
- Pale, washed-out materials
- Color texture loaded as UNORM. Byte 128 used as 0.502 albedo instead of 0.216.
- Tints too bright
- Color from a color picker used as a linear factor. The picker's 128 is 0.216 linear, not 0.5; decode it first.
- Banding in dark gradients
- Lighting into an 8-bit linear target. Only 26 codes below 10% light (section 1).
- Wrong contrast
- Tone mapping after the encode. Tone map in linear, then encode.
This page is about the transfer function: the curve between light and code. A color space also fixes its primaries: which exact red, green and blue the channels mean. sRGB shares its primaries with Rec. 709 (HDTV). Wide-gamut and HDR displays use other primaries (Display P3, Rec. 2020) and other curves (PQ for HDR10). The rule stays the same: decode to linear, compute, encode for the display.
9In RASM
RASM follows the textbook pipeline with 8-bit textures, a 16-bit float scene target and an 8-bit sRGB swapchain. The GPU decodes color textures and encodes the output. Shaders work in linear only. The material shaders write unbounded light, and one tone-map pass brings it to the swapchain.
R8G8B8A8_SRGB, which encodes the same way.Where RASM follows the textbook, and where it differs
| Part | This page | RASM |
|---|---|---|
| Color vs data textures | section 4 | glTF: base color and emissive are uploaded as R8G8B8A8_SRGB, metallic-roughness, normal and occlusion as R8G8B8A8_UNORM (loadMaterial). loadTexture takes the format from the caller, R8G8B8A8_SRGB by default, and refuses any other format than these two. A KTX2 texture follows the slot too, not the file: a color slot gets an sRGB format (BC7_SRGB, ASTC_4x4_SRGB, …), a data slot a UNORM one, whatever transfer function the file names (texture compression). |
| Decoding | by the format, before filtering | The same: the sampler decodes _SRGB textures. No shader calls the curve. |
| Mips | averaged in linear | Yes, for sRGB formats (see mipmaps). |
| Factors | linear | glTF factors are linear and copied as they are. Hand-made materials start with white base color (createMaterial); a color typed in by hand must be linear. |
| Lighting | linear | Yes: shadePBR in shaders/lib/pbr.slang. |
| HDR target | 16-bit float, tone map in a final pass | The same: the scene passes draw into a SCENE_COLOR_FORMAT (R16G16B16A16_SFLOAT) target, and Renderer::addToneMapPass maps it to the swapchain in a pass of its own. |
| Tone mapping | exposure + a curve, before the encode | The camera's exposure, then AgX (tone mapping). Unlit materials and the clear color go through the same curve, so an unlit texture does not show its exact colors. |
| Output and encode | an _SRGB target encodes | The swapchain asks for VK_FORMAT_B8G8R8A8_SRGB with VK_COLOR_SPACE_SRGB_NONLINEAR_KHR. The scene targets are float, with no curve; only the swapchain encodes. A headless engine draws into an OffscreenSwapchain of R8G8B8A8_SRGB images instead (headless rendering). |
| Blending | in linear, by the _SRGB target | The Transparent pass blends in the float scene target, so in linear light. |
| UI colors | codes, meant to be blended as codes | Dear ImGui's colors are sRGB codes. The UI pass draws through the swapchain's B8G8R8A8_UNORM view (headless, the OffscreenSwapchain images' R8G8B8A8_UNORM view, TextureDesc::encodedView), which stores and blends the codes as they are; an sRGB texture in an image is encoded back to codes in ui.slang. Without the view (D31) the shader decodes the colors and the blend is in linear light. See Immediate-mode UI. |
| Debug views | show values, not light | A debug view (Renderer::setDebugView) writes display values (a normal as a color, a count) into scene_hdr. The tone map then draws fragDebug or fragDebugHeat in shaders/passes/toneMap.slang: no exposure, no curve, and decodeSrgb first, so the swapchain's encode gives back the value as it is. |
| Linear constants | clear colors are linear | Renderer::render clears scene_hdr to BACKGROUND_COLOR, 0.018 linear, which then goes through the tone map. The same constant clears passes that draw straight to the swapchain, not tone mapped; the encode turns it into byte 36. The stats overlay's colors in statsOverlay.slang are linear too, and are not tone mapped. |
Where it lives in code
| What | Where |
|---|---|
| Formats | Format in include/rasm/types.h |
| glTF color vs data | AssetLoader::Impl::loadMaterial (its texture upload, with an sRGB flag per texture) in src/assets/material.cpp |
| Loading a texture file | AssetLoader::loadTexture in src/assets/texture.cpp; its default format in include/rasm/renderer.h |
| sRGB-correct mips | VulkanDevice::fillTexture in src/rhi/vulkan/vulkanDevice.cpp |
| Swapchain format | buildSwapchain in src/rhi/vulkan/vulkanSwapchain.cpp; headless, OffscreenSwapchain::FORMAT in src/rhi/offscreenSwapchain.h |
| Lighting | shadePBR in shaders/lib/pbr.slang; shadeUnlit in shaders/lib/unlit.slang |
| Tone mapping | Renderer::addToneMapPass in src/renderer/toneMap.cpp; the curve in shaders/lib/agx.slang, the debug views' display (fragDebug) in shaders/passes/toneMap.slang |
| Hand-made textures, both kinds | createFilledTexture calls in examples/05_city/main.cpp |
- Normal, metallic-roughness and occlusion textures are
R8G8B8A8_UNORM; color and emissive textures areR8G8B8A8_SRGB. - Shaders output linear color. Never apply the sRGB curve in a shader: the swapchain format encodes.
- The swapchain must be an sRGB format; with a UNORM one, the image comes out too dark.
- Tone map once, in the tone-map pass. Material shaders write unbounded linear light.
- Material factors are linear. Convert a color from an sRGB color picker before storing it.
No automatic exposure and no output to HDR displays. The swapchain format is a request: set_desired_format asks vk-bootstrap for VK_FORMAT_B8G8R8A8_SRGB, and nothing checks the format it really chose.
Further reading
- IEC 61966-2-1:1999, Default RGB colour space – sRGB: the standard that defines the curve.
- Larry Gritz and Eugene d'Eon, The Importance of Being Linear, in GPU Gems 3 (2007), chapter 24.
- Charles Poynton, Digital Video and HD: Algorithms and Interfaces, 2nd edition (2012): transfer functions and gamma.
- Erik Reinhard and others, Photographic Tone Reproduction for Digital Images, SIGGRAPH 2002.
- The Vulkan specification: "sRGB EOTF" in the chapter on formats, and the texel input and output operations.