Search Unity

Question Colors always storing in Gamma Space in compute buffer?

Discussion in 'General Graphics' started by carcasanchez, Feb 4, 2021.

  1. carcasanchez

    carcasanchez

    Joined:
    Jul 15, 2018
    Posts:
    177
    Hello, everybody. I have recently run into a curious color issue that, although having a very easy solution, the reasons are still unknown for me.
    See, I am working with GPU instancing, and I have a computer buffer of colors that I fill on start, and I read in shader to apply variations on the fly.
    The curious part, is that I have noticed a slight color deviation from the original to the instances:
    upload_2021-2-4_10-45-40.png
    The above model is the original. See the slight deviation in the red skin and in the green?

    I managed to find a solution, that is applying a GammaToLinear conversion when reading the color from the compute buffer, and the deviation goes. Easy.

    However, I still don't know why this happens. The project is in Linear space, and the buffer is just a Float4 buffer that gets filled with Color in the CPU side. In GPU, I just read the buffer and store the color again in a Float4.

    Is there any covered operation Compute buffers do, or what I am missing here?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    When assigning the color values from the CPU, you should be doing.
    Code (csharp):
    1. myBuffer[index] = myColor.linear;
    The color value you see and set in the inspector for things is going to be in sRGB gamma space. When assigning that to a material, Unity automatically handles the gamma conversion for you when it hands it off to the GPU. But when you're doing it through your own code, you need to handle the color conversion yourself.
     
  3. carcasanchez

    carcasanchez

    Joined:
    Jul 15, 2018
    Posts:
    177
    Thanks again, bgolus, that's pretty insighful.