Unity sRGB格式支持

测试的Unity版本 2021.03.32f1

RenderTexture

小结

1.设置sRGB为false(linear纹理),可直接根据GetLinearFormat()设置graphicsFormat,获取线性纹理格式
2.设置sRGB为true(sRGB纹理),则需优先判定Quality里设置是否为Linear空间且不属于R8、RG16格式,为Linear且不属于则根据GetSRGBFormat设置graphicsFormat

3.Unity代码里,IsSRGBFormat格式R16G16B16A16_SFloat是否为sRGB,未找到kFormatPropertySRGBBit标志,故而该格式不支持sRGB

使用

sRGB为函数式属性, 在该版本中get/set官方做了定义

测试调用时, 调用了GetCompatibleDescriptor()方法, 且QualitySettings里ColorSpace为Linear


R16G16B16A16_SFloat = 48, // 0x00000030, 该格式枚举索引值为48
GetCompatibleDescriptor(512, 512, RenderTextureFormat.ARGBHalf,0);
RenderTextureDescriptor GetCompatibleDescriptor(
    int width,
    int height,
    RenderTextureFormat format = RenderTextureFormat.Default,
    int depthBufferBits = 0,
    RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default,
    bool enableRandomWrite = false)
    {
        var desc = cameraData.cameraTargetDescriptor;
        desc.depthBufferBits = depthBufferBits;
        desc.msaaSamples = 1;
        desc.width = width;
        desc.height = height;
        desc.colorFormat = format;
        desc.enableRandomWrite = enableRandomWrite;

        if (readWrite == RenderTextureReadWrite.sRGB)
            desc.sRGB = true;
        else if (readWrite == RenderTextureReadWrite.Linear)
            desc.sRGB = false;
        else if (readWrite == RenderTextureReadWrite.Default)
            desc.sRGB = QualitySettings.activeColorSpace == ColorSpace.Linear;

        return desc;
    }
// this flag causes the render texture uses sRGB read/write conversions.
bool sRGB
{
    get
    {
        return GraphicsFormatUtility.IsSRGBFormat(graphicsFormat);
    }
    set
    {
        graphicsFormat = (
            (value && QualitySettings.activeColorSpace == ColorSpace.Linear && colorFormat != RenderTextureFormat.R8 && colorFormat != RenderTextureFormat.RG16) ? 
            GraphicsFormatUtility.GetSRGBFormat(graphicsFormat) : 
            GraphicsFormatUtility.GetLinearFormat(graphicsFormat));
    }
}
bool IsSRGBFormat(GraphicsFormat format)
{
    return (GetDesc(format).flags & kFormatPropertySRGBBit) != 0;
}

GraphicsFormat GetSRGBFormat(GraphicsFormat format)
{
    return GetDesc(format).srgbFormat;
}

GraphicsFormat GetLinearFormat(GraphicsFormat format)
{
    return GetDesc(format).linearFormat;
}

索引48枚举键值不含kFormatPropertySRGBBit标志位

{8, 1, 1, 1, kFormatSwizzleR, kFormatSwizzleG, kFormatSwizzleB, kFormatSwizzleA, kFormatR32G32B32A32_SFloat, kFormatR16G16B16A16_SFloat, kFormatR16G16B16A16_SFloat, kFormatR16G16B16A16_SFloat, kTexFormatRGBAHalf, kRTFormatARGBHalf, 3, 1, "RGBAHalf", kFormatPropertyIEEE754Bit | kFormatPropertySignedBit};    //kFormatR16G16B16A16_SFloat