Search code examples
unity-game-engineglslshaderhlslhlsl2glsl

HLSL vs GLSL, in writing Unity shaders


confused about the shading language to use ( HLSL or GLSL ), because of supporting platforms. HLSL resources are available in abundance, but I saw through the web that it's specific to DirectX (windows). Would that be a problem if I'm planning to run my application on android? -I can't rely completely on ShaderGraph-

Thanks


Solution

  • You can use HLSL in Unity on Android platforms. From the documentation:

    HLSL syntax

    The HLSL language itself has two syntaxes: a “legacy” DX9-style syntax, and a more modern DX10+ style syntax.

    The difference is mostly in how texture sampling functions work:

    • The legacy syntax uses sampler2D, tex2D() and similar functions. This syntax works on all platforms.
    • The DX10+ syntax uses Texture2D, SamplerState and .Sample() functions. Some forms of this syntax do not work on OpenGL platforms, due to how textures and samplers are not different objects in OpenGL. In Unity, you can avoid problems with HLSL syntax platform support by using predefined macros to declare and sample textures. Unity expands these macros to the most appropriate syntax, depending on the platform that the shader is being compiled for.

    Shader compilers

    Different platforms use different shader compilers for shader program compilation as follows:

    • Windows and Microsoft platforms (DX11, DX12 and Xbox One) all use Microsoft’s HLSL compiler (currently FXC / D3DCompiler_47).
    • OpenGL (Core & ES), Metal and Vulkan use Microsoft’s HLSL followed by bytecode translation into GLSL, Metal or SPIR-V, using HLSLcc.
    • Other console platforms use their respective compilers (e.g. PSSL on PS4).
    • Surface Shaders use HLSL and MojoShader for code generation analysis step.