Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Gamma vs Linear Space

Discussion in 'Shaders' started by MoistDumpling14, Jan 16, 2019.

  1. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Hey everyone, simple question/discussion. Right now I am using gamma color space but am wondering if I should switch to linear. I believe this does not affect your final game and it is only for the scene view space but I just wanted to know. Let me know what you guys use. Thanks.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,617
    What's your target platform?
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,751
    You are wrong. It has effect on every part of your visuals. In a nutshell:
    - computers usually store lighting information in a srgb (gamma corrected) fashion
    - cameras get the information in a linear fashion (the more light hit the sensor of the camera the higher the number it stores, linearly)
    - human perception is closer to a logarithmic curve, we see smaller changes in the darker areas and less in the brighter ones (evolutionary advantages: we saw the predators move in the dark so they didn't eat us)
    - the gamma you see in unity settings means that the settings and the textures you're using was prepared in this gamma-corrected colorspace, every change was made in it
    - the linear colorspace setting roughly means that you store the lighting information as is, you make your changes in physically closer to the realistic colors, so you can apply realistic effects and you convert the results once before you show to the viewers

    more information and more explanation here (they are not Unity tutorials)

     
    Last edited: Jan 17, 2019
    ShantiB95, DhiaSendi, deus0 and 6 others like this.
  4. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Alright, so basically computers can't display all the colors that we can see so it uses a little trick called gamma correction to make it look right. So my one question is how this relates to unity. Does unity automatically use gamma color space or do I have to change something to make it do that whether it's the lighting or the materials. Thanks for the help btw that first video was really good.
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,751
    gamma-linear.PNG

    You choose if you want Unity to store your lightning already in gamme corrected (Gamma). Or you can choose to store in Linear (Linear). Some devices don't support Linear (usually older or some mobiles).
    If you choose Gamma, you get more "plastic"-like colors at the end, because everything is happening in gamma-correcter color space. All your changes, effects, etc. Basically Unity store everything in srgb and just shovel it out to the GPU.
    If you choose Linear, everything will happen in physically more correct fashion (this is why the screenshot in Linear color space are more "natural"), so we can apply effects on more realistic lighting values. And the gamma correction will happen at the end, when everything is set (so the monitor can display the image).
    It's not perfect, so you can't apply everything physically real (like for example sun light), but it's close enough.
    Obviously Linear is a little bit more resource-hungry, so better mobiles or better desktops usually support this or higher.

    Also obviously my explanations are simplistic so it's just vague approximation what's happening really. :) You can find a ton of resources online and in books usually related to lighting.
     
  6. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Wait so then what is the point of using gamma if linear is just what we see as is. I understand that gamma corrects the color to make it easier for us to see and understand the colors but is that just it? The linear looks more like the actual real life thing however, it's hard to distinct the colors and values so computers change it to gamma? Like for example what do good games use and what do you personally use as a color space and why do you use it?
     
  7. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,617
    Gamma is how we used to do things. All older games use Gamma.

    Gamma is also usually faster than Linear.
     
  8. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Alright so you suggest I use linear? Why would unity default to gamma though?
     
  9. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Ok, please explain this to me like I am an idiot. What is linear color space and what is gamma. I am soooo confused right now. I'm sorry I don't get it I just really do not understand this at all.
     
  10. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Woah hold on I think I got it. Gamma color space is how are eyes see things in the real world because our eyes do not perceive light in a linear fashion. So that's why gamma is so commonly used, however, linear is what the world actually SHOULD look like to our eyes so when you enable that color space everything looks the way that it naturally would. The game is in linear and then gamma correction is used to make it look like how the real world looks like to us and corrects it. Please tell me that's right
     
    Misnomer, RatherGood and halberd19 like this.
  11. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    So for example this image right here... the gradient on the left is linear and how the world should look(aka linear) and then the image to the right is how are eyes perceive that gradient(aka gamma). So what happens is that the computer receives the image as linear(aka as it is) and then changes it to gamma(aka the image on the right) so that it looks correct to our eyes... just like how we see the real world.
     

    Attached Files:

  12. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    So if I use linear and then publish my game will it correct to gamma? Also what about my textures, will those be displayed in linear or gamma.
     
  13. Chuptys

    Chuptys

    Joined:
    Mar 19, 2014
    Posts:
    18
    The final colors (and textures) you see on screen are always gamma-corrected. Choosing gamma or linear space does not affect this.
    The only difference is that, when choosing linear, all colors are handled in linear space during lighting calculations (so more physically accurate). When the calculations are done, the result is converted to gamma space for displaying on screen.
     
  14. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    So when I am making my game in linear, the final product isn't any different than what it would be if I was working in gamma. It's just easier to see lighting calculations during the making of the game. So why would you use that then? It doesn't really make sense if the final product is going to be gamma anyway. And what exactly does physically accurate mean?
     
  15. Adam-Bailey

    Adam-Bailey

    Joined:
    Feb 17, 2015
    Posts:
    232
    Think of it as like rounding errors. Converting to gamma reduces the precision/accuracy of the information. So as part of the rendering and lighting calculations you are getting further and further from the "correct" result each time those inaccurate values are used. It's a destructive process.

    By working in linear space you keep as much accuracy as possible right up to the end.
     
    radiantboy and MoistDumpling14 like this.
  16. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You should always pick linear. There is no longer any sane reason to use gamma IMHO. You can argue that some crappy old mobile might need gamma, but it's not worth the hassle. Performance difference is too small to worry about as well.
     
    Rickyl likes this.
  17. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    Easiest analogy is for me: Linear is like recording/shooting in raw. You'll get more dynamic range and more control over the endresult. If you got an over or underexposed spot in a scene you can't get any more detail out of it no matter how much you post process.
     
  18. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    100% incorrect. Working in linear means the full range of colour is able to be converted. Working in gamma is like saving over the same jpg over and over, it's lossy. you clip half the colours and it looks like retro butt, and you get all this banding and lack of colour. I hope that makes it clearer.

    In addition your post effects will also suck due to lack of colour information. All AAA games use linear.

    TLDR: it's dumb to go with gamma if your project has the option of using linear.
     
  19. Adam-Bailey

    Adam-Bailey

    Joined:
    Feb 17, 2015
    Posts:
    232
    There are some situations like mobile VR on the GearVR/Go where quite recently Oculus was still recommending Gamma due to driver issues on some chipsets, but yeah for the most part Linear should just be a switch it on at the start of development by default kind of thing.
     
    hippocoder likes this.
  20. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    There's still driver issues? ugh, just they need to fix that nonsense.
     
    MNNoxMortem likes this.
  21. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,313
  22. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Wait so if my game is in linear and it's not gamma corrected then shouldn't that look wrong to our eyes?
     
  23. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Thank you so much. Actually that makes sense. You do all your work in linear to stay completely accurate and then at the end it gets converted to gamma. But if you work in gamma the lighting keeps on getting corrected, and corrected and corrected the complete wrong way. My one question... if you are working in linear I know the final result gets converted, but if you are working in gamma does the same thing happen. Do you get a double gamma correction or does it just leave it as is.
     
  24. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    I like that
     
  25. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    That makes sense, thanks.
     
  26. iliasM

    iliasM

    Joined:
    Dec 15, 2017
    Posts:
    15
    I make an AR mobile application using ARcore in unity 2018.3. I use speedtree models and I have reached the conclusion that linear is much better than gamma for my models.Could Linear make "heavier" - slower my mobile App?
     
  27. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    No, not on anything except devices which can't do AR to begin with.... :)
     
  28. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    I can attest there is absolutely no performance difference between Gamma and Linear color spaces on the Oculus Quest with the built-in forward renderer on Unity 2019.3.5f1
    It could, however, make transparencies slower as per this report of Oculus, but I haven't tested it.
     
    Last edited: Mar 30, 2020
    glenneroo and hippocoder like this.
  29. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    I have just done a stress test of Gamma vs Linear color spaces with transparencies and Gamma is indeed a liiiitle faster than Linear, but the difference is so minimal it doesn't even matter. (2 to 3 frames per second more on a stress test that was overdrawing 8 transparent planes one over the other for the full screen space.)
    On any GPU that handles GLES3 there is no performance reason to use Gamma space over Linear.
    Gamma space is legacy now and is only useful for legacy GLES2 GPUs (because linear requires GLES3)
     
  30. DonPaol0

    DonPaol0

    Joined:
    Sep 30, 2019
    Posts:
    11
  31. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    On Quest I used linear and had no problems I could tell, perhaps there's a transparent cost, I don't know I barely use transparencies on Quest for obvious reasons :)

    Games like red matter use linear but fake transparency, so this could be a contributory reason. To my mind I can't justify the quality loss of gamma.

    Also @phil_lira could you please let us know if URP is affected by conversions in the same way?
     
  32. DonPaol0

    DonPaol0

    Joined:
    Sep 30, 2019
    Posts:
    11
    @hippocoder: I actually did this test in URP. Regarding transparencies on the Adreno 540: Yes, they are bad, but geo foliage is even worse at the density I want to achieve. How does Red Matter fake transparencies? Sounds interesting :)
     
  33. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    I guess by either fetching the framebuffer or GrabPass. This isn't cheap either, but since it's technically an opaque render using the framebuffer as a background texture, you will not have several transparent layers one over the other, speeding things up.
     
  34. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,617
    Well...

    If you are releasing to something like Android, I would actually suggest staying in Gamma. Unity has had issues with Linear for years. Some odd device would show the wrong colors <- This bug keeps coming up even since Unity 5, some odd devices would have terrible performance, lightmapping had issues.

    But that could be me just being paranoid about using as little Unity features as possible. The more *advanced* it seems the more chances it has to break randomly. I thought controller support for iOS/tvOS would never be broken on LTS, but they broke it. So with that in mind: Gamma for less surprises and maybe faster performance.
     
  35. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    I will have to check this out again!
    I tested it with alpha test and alpha blending, but not with alpha to mask (which is what I'm using now btw)
    I'll also check this sRGB conversion flag to see on the other thread on my Quest:
    https://forum.unity.com/threads/is-...gamma-lighting-on-mobile.651073/#post-5823481
     
  36. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Alpha testing disables optimisations on this range of GPUs so it's likely to be quite slow and force tile resolves too, so that's also something to avoid.
     
    AcidArrow likes this.
  37. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    I know, I tried :p : Only using alpha to coverage now and it's prettier and way faster! ;)
     
    hippocoder and AcidArrow like this.
  38. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Alpha To Coverage still will disable optimisations for tile GPUs.

    I haven't checked this on Quest (Adreno 540) regarding forced tile resolve... I imagine though it just is disabling optimisations and has a similar perf hit to blending. But if it's causing tile resolves you'll know pretty quick.
     
  39. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    Ok, I just rechecked Gamma vs Linear on a heavy overdraw setup with alpha to coverage / alpha to mask.
    Same exact scene, same exact camera orientation.
    The results are:

    GAMMA: 48 fps
    LINEAR: 52 fps


    So, Linear is actually FASTER than Gamma on an Oculus Quest with Built-in forward render and 4xMSAA.
    Also, Display.displays[0].requiresSrgbBlitToBackbuffer returns FALSE, which means it doesn't have to blit from linear to the screen. (this is good)
    EDIT: It seems like the Oculus Quest is still using a blit anyway.
    Linear being faster isn't a surprise, since internally Unity's shaders will put extra instructions to convert from linear to gamma for every pixel drawn. (Unity uses linear space internally and converts to gamma on output)
    So on hardware that supports Linear Color Space, it actually bypasses the shader gamma conversion on output.
    In the case of @DonPaol0 , it seems his scene is using a blit to render to screen, so something else may be happening. Maybe it's an URP thing. On built-in forward it doesn't blit.

    As @aleksandrk said in another post:
    So this may be tricky on other Android devices, but on the Oculus Quest, Linear Color Space is definitively not only supported but actually faster and looks nicer.
     
    Last edited: May 10, 2020
    glenneroo, Vivek-Savsaiya and bgolus like this.
  40. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's what I've been saying to cries of disbelief on the forums for a long time. Perhaps people have different experiences because their projects use all sorts of assets or shaders. That sort of thing can easily run away from a developer's control.
     
    atomicjoe likes this.
  41. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    Alpha to coverage is indeed slower than a regular opaque render, but since it writes to the z-buffer, it is actually much faster than a regular alpha blending because it will discard pixels that are rendered behind opaque zones. (assuming you use it with a texture that has full opaque zones, that is)

    EDIT: it's faster for the scene render as a whole
     
    hippocoder likes this.
  42. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Unfortunately, that's not correct to my knowledge. This particular optimisation is something you give up. If you could test it to see if it's something the 540 GPU / drivers handle then I can be corrected on it, but when I read up on it a few years ago, it was something that disabled this particular optimisation..

    If it manages to also reject pixels then I'll be surprised and delighted so long as it's not at cost of a resolve.
     
  43. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    I can assure you it's the way it runs on the Quest.
    The OVR Metrics Tool doesn't lie.
     
    hippocoder likes this.
  44. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    When I'm in front of full opaque zone, it prevents the geometry behind it to render.
    And when you check how alpha to coverage works, it actually makes sense, since it's writing to the z-buffer at a higher resolution, but still writing full opaque or full transparent values.
    What it will not do for you is discard geometry that is behind your polygon but has been rendered before this polygon.
    This is something only PowerVR GPUs do.
    So I just forcibly set the render order of my gameobjects from front to back, since Unity seems to don't care about it at all... :rolleyes:
     
  45. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,617
    Maybe it's different in the Quest, but for certain devices (specifically PowerVR chips, but also many more), using alpha test undos some optimisations.

    Now it's been a while, but vey vaguely how I remember it is that the GPU figures out what pixels to render by doing a quick z-buffer pass of all opaque geometry, without running any frag shader, and then it goes to render frag, and if there's a z-test shader, it goes *oh I guess I did all that z-buffer work for nothing, I have to re-do it now*.
     
  46. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    Yes, PowerVR does that. Adreno doesn't.
    Anyway, I'm not talking theoretically, I'm talking from actual experience right now on the Quest.
    It's clear as water: I put a lot of planes one in front of the others with a cutout texture on all of them and use a custom alpha to coverage shader on them.
    When I'm in front of a full opaque zone that hides the other planes behind, FPS is high.
    When I'm in front of a full transparent zone, FPS goes VERY low.
     
    AcidArrow likes this.
  47. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    What disables those optimizations is using DISCARD (or Clip, it's the same internally).
    Using Clip or Discard on your shader will tank your framerate, BUT,
    If you just use Alpha to Coverage and DON'T use any Clip or Discard function, it's actually as fast as an alpha blend plus it writes to the z-buffer and prevents farther geometry to be drawn.

    I have a feeling nobody actually tries this kind of things. :p
     
  48. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,617
    I am too tired to have a serious conversation about this, but : Isn't alpha to coverage using alpha testing, which means discard?
     
  49. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,690
    It seems not. Maybe there is an internal optimization. I don't know, I don't design GPUs :p
    What I can tell is: use Clip or Discard, your framerate goes to hell. Use only alpha to coverage and it doesn't.
     
  50. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,617