Search Unity

Is writing shaders in CG or GLSL a better idea?

Discussion in 'Shaders' started by techmage, Apr 25, 2014.

  1. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    I remember a while back reading somewhere that if your target output is iOS, it's better that you write your shaders in GLSL. As GLSL will give you better performance and more functionality.

    So I wrote some shaders in GLSL just because.

    But I see that every advanced shader package outputs CG code. It made me wonder. Even if my target is only iOS, Android, OSX and Linux, and I could get away with OpenGL only if I ever needed to go on Windows. Is writing my shaders in CG a good idea or not?
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    CG without question. GLSL won't even work at all on some platforms, and it's not faster in the slightest. Old news is bad news. Just avoid surface shaders like the plague if you want speed.
     
  3. SurvivalMachine

    SurvivalMachine

    Joined:
    Jun 21, 2013
    Posts:
    15
    GLSL is better. CG compiler sometimes generates code that is not optimal for iOS GPU, specifically code that uses dependent texture reads that are not necessary.
     
  4. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Write it in CG unless you're absolutely sure it's not optimising the GLSL translation as well as it should and you're certain you really need the speed-up from implementing it yourself.
     
  5. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    So in what instances is the difference between GLSL and CG non existent, and in what instances is it? Has anyone done much testing with this stuff?
     
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    GLSL and CG are pretty different. Unity will convert your CG shader to GLSL if you use
    #pragma glsl
    otherwise it gets converted to OpenGL ARB.
     
  7. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    How would you even be able to tell that it's not as optimized as it could be?
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    You can open up the compiled shader and see the code it generates.
     
  9. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Seems like something you can only really judge if you know the hardware/drivers better than the compiler does. And if your tests show that the code runs slower than it should.
     
  10. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Yeah, pretty much. But I've noticed it can generate code with some variables that aren't really needed sometimes... but honestly nothing I think would be noticeably detrimental to your framerate.
     
  11. AxisRob

    AxisRob

    Joined:
    Aug 6, 2013
    Posts:
    33
    I`m going to necro this, because I have a question that I think is best served here.

    Despite CG being officially abandoned by NVIDIA (years ago), do you think it`s still wise to learn (over GLSL, which they recommend)?
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    CG. It's based on HLSL in Unity 5 onward.
     
  13. AxisRob

    AxisRob

    Joined:
    Aug 6, 2013
    Posts:
    33
    So stick with CG, and it works in Unity 5?

    Excellent, thank you!