How DirectX Handles sRGB to Linear Gamma Conversion

DirectX manages color space conversion between sRGB and linear gamma through dedicated hardware-accelerated pipeline features and specific texture formats. Instead of requiring developers to manually compute gamma curves within shader code, modern DirectX APIs (DirectX 11 and DirectX 12) utilize the Direct3D graphics pipeline to automatically decode non-linear sRGB image data into linear floating-point values during texture sampling, and re-encode linear values back to sRGB when writing to the final render target.

Hardware-Accelerated Formats via DXGI

The core of color space handling in DirectX is the DXGI_FORMAT enumeration. To handle non-linear color assets correctly, DirectX provides explicitly typed sRGB formats, such as DXGI_FORMAT_R8G8B8A8_UNORM_SRGB and DXGI_FORMAT_B8G8R8A8_UNORM_SRGB.

When an asset stored in standard sRGB color space is loaded, it must be assigned an _SRGB format. This instructs the GPU that the raw byte data (0 to 255) represents non-linear values governed by the standard sRGB transfer function (roughly equivalent to a 2.2 gamma curve).

Texture Sampling and Linearization

When a shader samples a texture flagged with an _SRGB format, the GPU's texture sampling unit automatically performs the conversion from sRGB to linear space before exposing the color data to the shader register:

  1. Hardware Decoding: The hardware applies the exact IEC 61966-2-1 sRGB piece-wise transfer function, converting the 8-bit non-linear integer data into 32-bit linear floating-point values.
  2. Filtering in Linear Space: A critical advantage of this hardware handling is texture filtering. DirectX performs bilinear, trilinear, and anisotropic filtering after the values have been converted to linear space. This eliminates common artifacts such as dark halos and incorrect luminance transitions along filtered texture edges.

Inside the HLSL shader, developers can perform all physically based rendering (PBR), lighting calculations, and additions in mathematically correct linear space without writing manual gamma correction formulas.

Render Target Writes and Blending

To display linear computations correctly on standard monitors, the colors must be converted back to non-linear sRGB. DirectX manages this at the Output Merger (OM) stage through sRGB render targets:

  • Alpha Blending: If hardware blending is enabled, DirectX executes the blend equation in linear space. The existing linear value in the render target and the newly calculated linear pixel from the shader are combined accurately.
  • Gamma Encoding: Upon the final write to a render target surface configured with an _SRGB format (such as an sRGB swap chain back buffer), the GPU hardware applies the inverse sRGB transfer function. This converts the linear floating-point color back to gamma-encoded sRGB values for display output.

Manual Shader Conversion

If a pipeline does not utilize _SRGB DXGI formats—such as when handling raw buffers or custom packed formats—DirectX shaders can manually implement the conversion in HLSL. The accurate piece-wise sRGB-to-linear conversion function is:

  • For values \(\le 0.04045\): \(\text{Linear} = \frac{\text{sRGB}}{12.92}\)
  • For values \(> 0.04045\): \(\text{Linear} = \left(\frac{\text{sRGB} + 0.055}{1.055}\right)^{2.4}\)

While a simplified power approximation (\(\text{pow}(\text{color}, 2.2)\)) is sometimes used for lower computational cost, utilizing the native _SRGB hardware pipeline formats remains the recommended approach in DirectX because it provides standard compliance, eliminates shader instruction overhead, and guarantees correct linear filtering.