Jump to content
  • Advertisement
Sign in to follow this  
Nairou

HLSL vs Cg: Any significant difference?

This topic is 4970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!