Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Tex2D (A8 / R8) use correctly? UNorm vs sRGB?

Discussion in 'General Graphics' started by Torbach78, Sep 3, 2022.

  1. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    grabbing ___.r or ___.a causes horrible banding

    Code (csharp):
    1. fixed4 mainColor = tex2D(_MainTex, uvoft);          
    2. fixed4 col = mainColor.r;
    3. return col;
    what do i need to read up on / examples with Tex2D() that explains UNorm conversion?

    even if i tell the texture Type 'Default' and sRGB = true
    output is clearly banding and brighter when compared to RGBA input and

    Code (csharp):
    1. fixed4 mainColor = tex2D(_MainTex, uvoft);          
    2. return mainColor;

    i hoped to save memory with uncompressed single-channel format but the UNorm is linear color space? rather than gamma

    (the banding is horrific and i'd rather learn to fix this code than go back to photoshop and curve my texture with a manual gamma)

    ---- I tested pow(col, 1.5) , yeah returns visuals ~sRGB 32bit [but a waste of arithmetic]
     
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    651
    I'm guessing that there is a misunderstanding of how SRGB works somewhere:

    - UNORM just means that the values are gonna be between 0 and 1
    - UNORM is separate from SRGB. For example, there is DXGI_FORMAT_R8G8B8A8_UNORM and DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
    - If you use a _SRGB texture format, you're declaring that the texture is in SRGB format. In that case, the GPU will do a linearization when you're sampling the texture. If you don't use a _SRGB format, there won't be any conversion when sampling.
    - You also have to be aware what the texel format of the render target is. If it's a SRGB format, the GPU will do a gamma conversion on the value that's returned from the pixel shader.
    - The alpha channel is always linear.
    - Floating point texture formats are always linear
    - Normal image files are almost always in SRGB format unless the texel values are not colors like in a normal map.

    So if you have a SRGB texture and a SRGB render target and you're not doing any calculations with the value in the shader, it's just a lossless copy.
     
    Torbach78 likes this.
  3. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    ok thanks for all this

    --- I have to conclude Type (Default) sRGB=True is erroneous if you assign single channel format

    Types both R8 or A8 single channel have no sRGB boolean anyway; They must be a linear formats and the practical solution would seem to manually gamma factor down my alpha8 textures in image editors


    edit
    im wrong, it seems without correcting fragment using a pow(input,x) i just can't get it to work visually how I want


    fixed4 mainColor = tex2D(_MainTex, uvoft);
    fixed4 col = saturate(2.0f * i.color * _TintColor * pow(mainColor.a,2.0));
    return col;

    well... it visually works
     
    Last edited: Sep 3, 2022