HLSL vs Cg: Any significant difference?

Started by
14 comments, last by NLDEV 19 years, 5 months ago
From what I understand so far, both HLSL and Cg accomplish exactly the same thing, and both are compiled into the same form of shader assembly code. Does this mean that, really, there is no significant difference between them? Can a shader implementation use the compiled code from either language identically, without regard to which language it was compiled from? If so, are HLSL and Cg nothing more than ways of making shaders easier to write, than actually adding features? If neither really adds anything new, why do we even have the two languages, as opposed to one? How different could they really be from each other?
Advertisement
The reason they both exist is because they were brought out by two seperate companies. Cg was NVIDIA's creation; HLSL is Microsoft's.

NVIDIA have all but stopped supporting CG these days. Most people programming for Win32 use HLSL with DirectX; people on other platforms can use GLSL with OpenGL.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

The syntax of the two languages is nearly identical - most Cg code will compile with the HLSL compiler with minimal changes and vice-versa. Either compiler will generate more or less the same shader assembly for DirectX and the compiled shader code is independent of the language used to generate it. The difference lies in things like the constant tables - mapping shader input variables to constant registers will require different CPU side code for Cg and HLSL.

Microsoft and nVIDIA collaborated on the language design and development. The main reason Cg exists is that nVIDIA wanted to release it some time before HLSL was due to appear in DirectX and also wanted to support OpenGL and DirectX with the same language. At the time there was no GLSL and clearly MS weren't going to support HLSL for OpenGL.

The languages are 'nothing more than ways of making shaders easier to write' but don't understimate the value of that - C++ (and all other high level languages) is nothing more than a way of making assembly language easier to write but that doesn't make it useless. The reasons for the existence of both languages (really both variants of the same language) are historical / political and they're really not very different from each other.

Game Programming Blog: www.mattnewport.com/blog

superpig: I thought CG development still go on. They released a beta version some weeks ago...
Gregory Jaegy[Homepage]
Quote:Original post by gjaegy
superpig: I thought CG development still go on. They released a beta version some weeks ago...
Really? They're certainly not making a big deal of it any more - they're more pushing their HLSL-based tools, like FX Composer.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Awesome, thanks guys! That clears it up for me. I didn't know GLSL existed, thats good to know, I guess I'll just focus on HLSL then. :)
Just to be sure: I think I read that old hardware supports Cg but not GLSL.

Is that correct? Does it make sense to support CG and GLSL in one engine then?
Writing errors since 10/25/2003 2:25:56 AM
Quote:Original post by Clueless
Is that correct? Does it make sense to support CG and GLSL in one engine then?


Personally I would just choose one and stick with it. GLSL requires a ARB_vertex_shader/ARB_fragment_shader level card--Cg can compile some Cg shaders down to a vp20/fp20 (NV register combiners) level card--of course I doubt you'll be able to compile all of your shaders to that level. At least I've had some problems with shaders that compile fine for the arbfp1 that don't like compiling for a GeForce 4.

But I digress--If you need support for older cards and/or API independance, go with Cg. Else use GLSL.

That's just my opinion, though. [smile]
Correct me if I am wrong here, but for those who want to go for API independence, neither HLSL or GLSL is an appropriate choice, correct? Afterall, no one wants to write two different versions of the same shader for their game.
Quote:Original post by Anonymous Poster
Correct me if I am wrong here, but for those who want to go for API independence, neither HLSL or GLSL is an appropriate choice, correct? Afterall, no one wants to write two different versions of the same shader for their game.


You are correct, HLSL and GLSL are specific to Direct3D and OpenGL, respectively. However, Cg (though being an almost indentical language as HLSL) has runtime bindings for both Direct3D and OpenGL--you can use the same shader code for either API with Cg.

This topic is closed to new replies.

Advertisement