Is writing shaders in CG or GLSL a better idea?

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?

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.

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.

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.

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?

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.

How would you even be able to tell that it’s not as optimized as it could be?

You can open up the compiled shader and see the code it generates.

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.

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.

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)?

CG. It’s based on HLSL in Unity 5 onward.

So stick with CG, and it works in Unity 5?

Excellent, thank you!