Search Unity

ShaderLab Confusion

Discussion in 'Shaders' started by rmele09, Jun 4, 2013.

  1. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    715
    I am just starting to learn about writing shaders in unity, and in general. I know that Unity allows you to use OpenGL and Direct X, but how does that work if you have to program in "shaderLab"? Isn't shaderLab a programming language? How does the workflow go if you want to use features form Direct X or OpenGL?
     
  2. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    ShaderLab is language agnostic, you program in something close to hlsl and it minds it's own business. You don't use DX or opengl features from shaders, the feature set on that side is pretty much the same unless you're trying to use DX11 feature (in which case shaderlabs has extentions that support that).
    So basically code in ShaderLab specifying which shader version you support and have it work automagically on all platforms.
     
  3. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    715
    So let's say I want to release a game for PC and Mac. With ShaderLab, can I use Direct X features for the PC version, and OpenGL features for the mac version? Or would I have to learn both APIs completely separate?
     
  4. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    You're mixing shaders and APIs, shaderlab is a shading language for all supported unity API.
    So you're using unity, this means you need to learn NEITHER directX NOR openGL, Unity is an abstraction layer far far above those.
    You still need (if you want custom shaders) to learn shaderLab to write shaders, but shaders you write using shaderlab will work on all platforms at once (Direct X9, directX 11, OpenGL, OpenGL es, xbox playstation etc etc), it's the reason shaderlab exists.
    And shaderlab is pretty much HLSL wrapped around a nice syntax, so if you want to write shaders start with the unity documentation on shaderlab and to go further look up hlsl documentation.
    Is there anything in particular you're trying to achieve? Most likely a shader for your needs already exists.
     
  5. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    What if you use CG, instead of a surface shader? Is CG in general multi platform?
     
  6. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Only shaderlab works on all platforms (and gets compiled to the relevant shading language depending on the target platform), it's not about what is multiplatform but about what unity uses.
     
  7. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I don't fully understand what you mean here. Are you referencing the fallback?

    I understand that Macs use OpenGL, and windows uses DX(x). As far as I understand you would make a sub shader for both using CG(or shader lab, but focus on CG), correct? If so can you point me towards the differences and/or a comparison, it would greatly help my simple shader knowledge.
     
  8. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    When Unity `compiles` the shaderlab sourcecode it creates versions in a lower-level language that is specific to various platforms, so there will likely be an OpenGLES version, a directx version, an opengl version, etc. If you open a compiled shader (button in the inspector) you will likely see also some low-level-looking `assembly code` language stuff. So Unity takes care of making your high-level code be translated to run in different languages for different platforms, invisibly and behind the scenes. You only need to be concerned about whether the shader is correct syntax etc within ShaderLab, and that includes cg programs. Unity can translate the cg into glsl or whatever is needed, and optimize it for you where possible. So you can't really do something that is DX or GL specific - you have to let go of thinking in terms of targetting those graphics API and instead think of what you can do in Unity's API, ie shaderlab code. And then just trust is will work fine on all platforms. That said some platforms have problems when the graphics driver is missing some features or whatever, but that's a different problem.
     
  9. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    No you don't make a subshader for both, you make a single shaderlab shader (with shaderlab being hlsl, very close to CG, wrapped in other statements), you don't make any of those : CG, HLSL, GLSL, you just make "only" shaderlab, and shaderlab is basicaly some statements with hlsl inside, that gets generate by unity into all targets (glsl, hlsl etc) depending on your target platform, you do 0 cross platform work there, you do a single shader and it targets all platforms.
     
  10. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    I did not mean a surface shader, the way I was told to write shaders was to wrap CG code in shader lab. Not write in completely shader lab.

    Thanks for clarifying, I was beginning to worry I would have to port my shaders to all platforms. Wouldn't be a big deal now as mine are all simple, but wanted to get those worries off my chest for the future. Pretty impressive how deceiving(in a good way mostly) Unity can be about making stuff work behind your back. :)

    Edit:

    Ok, I confused myself about surface shaders for a moment, I'm pretty new to shaders and just learning API/terms.

    What is it called how I do shaders? I have a vert method and a pixel method, instead of a single method. Based off of UnityCookies separate tutorial series these are completely different things?
     
    Last edited: Jun 4, 2013
  11. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Surface shaders "is" hlsl wrapped in shaderlab statements, and hlsl cg are pretty much letter for letter the same thing, so what you're doing "is" writing shaderlab in what you describe, but it's not CG's portability that enable's this, it's shaderlab compiling it for the correct platform
     
  12. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    715
    So for that Direct X 11 Unity contest that happened a few months ago, how are people using the Direct X 11 features? You can use them with ShaderLab?
     
  13. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Yes, in shader lab you indicate the shader level you target, so the hightest shader level will only work under DX11, in which case you can specify other programs like the tesselation step
     
  14. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    715
    This sounds pretty cool. I am sure its very complicated, but I like how you can develop in one language and it runs on both platforms. Thanks for all of the info, cleared things up a bit. Anybody have a favorite beginner tutorial for learning shaderlab?
     
  15. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    They do the work of converting from one language to another for you, so afaik they convert your shaderlab's HLSL content to GLSL when needed, i don't know if it's really complicated on their side as there are existing tools that do this, so i assume they use one and didn't roll their own but i really don't know there. There's no magic there, it's GLSL on openGL platforms hlsl on windows for exemple, it's just made so you don't have to worry about it as long as you go the surface shader way.
     
  16. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Have you read the ShaderLab Documentation?
    Have you read it more than once? ;)

    Its actually surprisingly helpful, once you've read it a few times, though often you find it missing information that you then end up piecing together from other sources or simply by breaking down existing shaders. For example download the 'built-in shader' source for Unity's shaders they are often a good place to start learning, as well as the shaders and scripts with Image effects in Pro.

    I'm not sure why people keep saying ShaderLab is HLSL, I thought it was wholly CG based and though they might share much in common, its always the CG Tutorial book from nvidia that I go to first.

    Also just to muddy the waters further Unity is not as restrictive as DX for Win and Opengl for Mac, as you can use Opengl on Windows in both the editor and standalones. Check out the commandline options to see.

    Then there is also the fact that instead of using CG in your subshader you can choose to use GLSL instead, so technically you could write all of your shaders in GLSL, but then you'd have to force opengl mode on Windows and thats not possible in say the webPlayer.

    However as others have stated the concept of Shaderlab is to allow you to write shaders using fixed function or CG which Unity will then create the necessary files to ensure the shader runs on all API's (assuming it is supported). In addition ShaderLab offers surface shaders which takes the pain out of writing a collection of shaders for the same material which supports different lighting set ups, forward/deferred rendering path etc.

    Its a brave new world you are entering, its also horrible confusing and frustrating, but the rewards can be beautiful too ;)
     
  17. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    I think there is some confusion with ShaderLab being a replacement for CgFX, not Cg, and Cg looking pretty much exactly like HLSL.

    As far as I know, this is how the work is divided in Unity:

    • ShaderLab: Framework for all shader code + Fixed Function shaders. Processed by Unity
    • Cg: Default language used for Surface, Vertex and Fragment shaders. Is almost identical to HLSL. Compiled using Cg Compiler when Unity is in default mode, which creates code in GLSL and D3D Assembly (I'm not entirely sure about the exact output).
    • HLSL: Language used in Unity DX11 mode, mainly for compute shaders. Compiled using the HLSL compiler, in Unity's DX11 mode. I would like to know for sure whether or not all shaders are compiled as HLSL when in DX11 mode.
    • GLSL: Language used for testing OpenGL specific shaders, if you suspect the Cg compiler might be doing something wrong or if you despise DirectX, and any other possible environment that Cg might compile to in the future.
     
    Last edited: Jun 4, 2013