Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HLSL = CG = Shaderlab?

Discussion in 'Shaders' started by milkytreat, Mar 17, 2007.

  1. milkytreat


    Jan 15, 2007
    Ive been trying to wrap my head around shaders tonight.

    Ive read that HLSL and CG are actually the same language, just that CG can compile to directx and opengl. Is that correct?

    Furthermore ShaderLab is based on CG? So therefore can i loosely assume that HLSL shaders can be used in Unity?

    What work is involved in porting them, is it simply a matter of just pasting the HLSL between CGPROGRAM and ENDCG. (biting my lip while i press COMMAND+P)

    The reason this is important for me to understand is because Ive found alot more (free) HLSL tutorials on the net than CG (mostly because CG is a too common word for me to google effectively)
  2. Aras


    Unity Technologies

    Nov 7, 2005
    These are really similar languages, but not entirely the same. In majority of cases HLSL and Cg are the same, but there are some subtle differences.

    ShaderLab (and Unity's "shader") is more than just "hardware shader" - it's everything that is required to display something; so ShaderLab files are more similar to .FX files in the concept - they contain multiple effect implementations for different hardware, expose tweakable properties, they set various graphics device state, and can contain actual harware shaders (that you write with Cg) inside.

    Mostly yes. However, what most of the techdemos or tutorials on The Internets don't tell is: Writing a shader is easy. Integrating it with the engine, and especially lighting, is hard.

    For example, when you find some "bump mapping" shader in some tutorial, it assumes you only ever have a single directional light (or a single point light, ...). How to make it support all kinds of lights? With cookies or without? Attenuated or not? Vertex lights? Pixel lights? These are the hard questions. The way Unity does lighting in shaders is documented here and here.

    So in summary: the short answer is yes, HLSL and Cg can be mostly interchanged. Making a "proper" shader from most tutorials - the one that would handle all kinds of different situations - is an involved task in any engine I guess.
    jq911 and ironthronethrone like this.
  3. kurylo3d


    Nov 7, 2009
    how about some sort of tutorial video of someone actually porting a bumped spec shader from cg into the shader lab stuff and making it function in engine.

    Quite frankly im suprized that one has not been made... It would help out the community largely if the unity team were to make one.

    As it stands right now.. u pretty much have to be a rocket scientist to write a shader. At least thats how I look at it.
  4. Aras


    Unity Technologies

    Nov 7, 2005
    The thing is, the solution would be totally different depending on where you're porting that "bumped shader" from.

    When it comes to interaction with lights (and bumpmapping by definition needs some interaction with lights), there is no standard way of how to approach that. Each engine and tool handles it differently, and requires shaders to be written differently.

    In upcoming Unity 3.0, we're trying to make shaders much easier to write (the ones that need to interact with lighting), by generating all the repetitive code that handles lighting/shadow details. But the approach will still be Unity specific because see above - there's no standard approach on how to make shaders interact with lighting.

    As a small teaser, here's a very simple bumpmapped shader in upcoming Unity 3.0:
    Code (csharp):
    2. Shader "Bumped Diffuse" {
    3. Properties {
    4.     _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     _BumpMap ("Normalmap", 2D) = "bump" {}
    6. }
    8. SubShader {
    9.     Tags { "RenderType"="Opaque" }
    12. #pragma surface surf Lambert
    14. sampler2D _MainTex;
    15. sampler2D _BumpMap;
    17. struct Input {
    18.     float2 uv_MainTex;
    19.     float2 uv_BumpMap;
    20. };
    22. void surf (Input IN, inout SurfaceOutput o) {
    23.     o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    24.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    25. }
    26. ENDCG  
    27. }
    29. FallBack "Diffuse"
    30. }
    The actually interesting bits in the above are two lines with "o.Albedo" and "o.Normal", and they do just that - surface color comes from a texture, and surface normal comes from a normal map.
  5. Ailos


    Sep 14, 2011
    Ok, I understand this. But , in the case I have a .cginc, how should I create a Material to use this?

    Thank you !
  6. tatoforever


    Apr 16, 2009
    Code (csharp):
    3.        #include "ExampleName.cginc"
    4.        //Here you can add data setup/manipulation to pass to your vertex and fragment shaders
    5. ENDCG
    And then your CGPROGRAM will contain directives to compile your vertex and fragment programs
  7. rimiki


    Dec 30, 2014
    Hello Guys,

    I'm trying to convert code from GLSL to CG using Unity, any help to convert this two functions?
    TextureSize and Textureload
    TextureSize----> unity detect is as a function but in excecution I got undeclared identifier !

    Thank you for your help !
  8. 8Observer8


    Apr 29, 2015
    Aras, could you rewrite your messages for Unity 5.3.4 and Unity 5.4.0 Beta?
  9. cician


    Dec 10, 2012
    As nvidia is not supporting CG anymore, latest unity versions actually compile shaders using HLSL compiler and transform the resulting bytecode to GLSL.
    The CG shader code continues to work mostly unchanged. Nowadays you can take advantage of modern shader features, like compute shaders and tessellation, that weren't supported by CG, using HLSL syntax. On consoles there's probably some macro magic involved to hide compiler differences, but I don't have any experience with that.
    Writing Surface Shaders haven't changed much from Unity 3.5/4.x, except for using PBR lighting model and material attributes. The Standard Shader is not a Surface Shader and written at lower level, which I frankly find quite puzzling. Idem, why Unity Tech. haven't yet done something like Unreal's material editor.
    Like Aras said, most of the complexity is not in writing the shader itself, but with engine integration, which Surface Shaders abstract pretty nicely for relatively simple shaders.
    zzp_buaa, Alverik and 8Observer8 like this.
  10. anna703


    Mar 14, 2013
    Came looking here as I'm also hearing CG and HLSL described as the same thing, but from what I've read they are related but not the same thing.

    I've just started doing this tutorial for writing shaders in Unity, finding it pretty good so far: - though hasn't cleared up this question yet! It uses CGPROGRAM and ENDCG which makes me think I'm using the old CG, but it also calls it HLSL.
    8Observer8 likes this.