1. Unity 5.6 is now released.
    Dismiss Notice
  2. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  3. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  4. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  5. The launcher is currently pulling earlier versions of the content packages which may have errors, see post.
    Dismiss Notice
  6. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  7. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice

HLSL = CG = Shaderlab?

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

  1. milkytreat

    milkytreat

    Joined:
    Jan 15, 2007
    Posts:
    267
    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

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,522
    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.
     
  3. kurylo3d

    kurylo3d

    Joined:
    Nov 7, 2009
    Posts:
    1,118
    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

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,522
    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):
    1.  
    2. Shader "Bumped Diffuse" {
    3. Properties {
    4.     _MainTex ("Base (RGB)", 2D) = "white" {}
    5.     _BumpMap ("Normalmap", 2D) = "bump" {}
    6. }
    7.  
    8. SubShader {
    9.     Tags { "RenderType"="Opaque" }
    10.  
    11. CGPROGRAM
    12. #pragma surface surf Lambert
    13.  
    14. sampler2D _MainTex;
    15. sampler2D _BumpMap;
    16.  
    17. struct Input {
    18.     float2 uv_MainTex;
    19.     float2 uv_BumpMap;
    20. };
    21.  
    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. }
    28.  
    29. FallBack "Diffuse"
    30. }
    31.  
    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

    Ailos

    Joined:
    Sep 14, 2011
    Posts:
    13
    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

    tatoforever

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

    rimiki

    Joined:
    Dec 30, 2014
    Posts:
    102
    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

    8Observer8

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

    cician

    Joined:
    Dec 10, 2012
    Posts:
    230
    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.
     
    Alverik and 8Observer8 like this.