Search Unity

HLSL in Shaderlab

Discussion in 'Shaders' started by lhk, Dec 12, 2010.

  1. lhk

    lhk

    Joined:
    Dec 12, 2010
    Posts:
    9
    Hi,

    I've read here :
    http://unity3d.com/support/documentation/Manual/Shaders.html
    that Unity's shader can be written in HLSL or CG. However the rest of the reference doesn't mention HLSL anymore and everywhere I look I read that CG has to be used. It seems like the conversion from HLSL to CG is just a minor problem still I would like to use HLSL directly. The link above indicates that something like that is possible.

    I'm searching for something like
    Code (csharp):
    1. HLSLProgram
    2. //...
    3. ENDHLSL
    Is that possible ? If possible, what problems will arise due to use of HLSL ?
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I wonder if it's a typo. I don't think I've seen HLSL here, but GLSL is supported and undocumented. That page used to say "GLSL".
     
  3. lhk

    lhk

    Joined:
    Dec 12, 2010
    Posts:
    9
    Well, if there's no way of using hlsl in shaderlab, how much work is it to convert hlsl to cg ? I've heard they're pretty similar in syntax.
     
  4. lhk

    lhk

    Joined:
    Dec 12, 2010
    Posts:
    9
  5. nielsmillikan

    nielsmillikan

    Joined:
    Nov 29, 2010
    Posts:
    72
    HLSL and CG are the same except for a few functions that were introduced in CG later. HLSL was developed by microsoft and Nvidia together to be able to use high level languages to write instructions for the vertex processor as well as the shader processor in your GPU.
     
  6. lhk

    lhk

    Joined:
    Dec 12, 2010
    Posts:
    9
    Are there functions in HLSL that CG doesn't have ? If only CG has been extended I guess there should be no problems with just pasting HLSL between CGPROGRAM and ENDCG
     
  7. tokyob

    tokyob

    Joined:
    May 17, 2010
    Posts:
    37
    HLSL and CG are based on the same concept and "c" like language.
    Cg and HLSL were developed in parallel as a partnership between nVidia and Microsoft. Their syntax and semantics " originally" were identical.

    but as Microsoft decided to develop HLSL on it's own, there is lot of little diferences that makes CG and HLSL to be "incompatible".

    some basic function like mul(); doesn't work the same way. the parameter order are not the same:
    Code (csharp):
    1. [B]HLSL[/B]: mul (matrix, vector);
    2. [B]CG[/B]: mul(vector, matrix);
    plus each language have some specific functions, formats and types...

    so you can't Copy/Paste HLSL into CG like that. it's need some correction.
    i think you must have some HLSL tags for unity to compile correctly....
     
    Last edited: Dec 13, 2010
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    mul() has three different overloads in Cg and HLSL, for matrix-vector, vector-matrix and matrix-matrix multiplications. The order in which you need to multiply vectors and matrices depends on the data in the matrix. I'm not sure if Unity transposes the built-in matrices for HLSL or not. If it does, then you would need to swap your matrix multiplications. If it doesn't, then you can leave the Cg as-is.
     
  9. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    The multiplication order only depends on whether your matrices are column major or row major. There's no "one true way" of doing, but it's true that most Cg shader examples choose different order compared to most HLSL examples. So the choice does not depend on a language per se, as more of "what convention is used".

    The Cg and HLSL languages (for D3D9 level stuff) are identical for all practical purposes. They started diverging a bit with Direct3D10 and later stuff.

    In Unity's Xbox 360 build, we compile exactly the same regular shaders with Xbox360's HLSL. So yes, the language is the same for practical reasons. The conventions might differ of course.
     
  10. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    That answers the question I posed: Unity does not transpose matrix data when compiling HLSL shaders, which means you should use mul(M, v) when transforming a vector with a matrix.