Gamma issue when display linear color

Hi, I'm displaying linear gray by CAMetalLayer with the shader below.

fragment float4 fragmentShader(VertexOut in [[stage_in]],
                               texture2d<float, access::sample> BGRATexture [[ texture(0) ]])
{
    float color = in.texCoordinates.x;
    return float4(float3(color), 1.0);
}

And my CAMetalLayer has been set to linearSRGB.

metalLayer.colorspace = CGColorSpace(name: CGColorSpace.linearSRGB)
metalLayer.pixelFormat = .bgra8Unorm

Why the display seem add gamma? Apparently the middle gray is 187 but not 128.

Replies

You are sending an srgb format and specifiying a linear colorspace. bgra8unorm means the data is typically stored gamma space. bgra8unorm_srgb converts to linear on reads and would match your color space.

  • Even I'm sending the sRGB format and specifiying a linear colorspace, the texture contains linear data right? And system won't do any Gamma correction since I specify it as linear colorspace. But why system add Gamma at the last and make it brighter?

Add a Comment