Search Unity

CG compiled to GLSL: hyperbolic functions not found.

Discussion in 'Shaders' started by RunSwimFly, Apr 16, 2015.

  1. RunSwimFly

    RunSwimFly

    Joined:
    Mar 2, 2011
    Posts:
    34
    I'm using tanh (extensively for neural net simulation) from a CG shader. Under Unity 4 on my Macbook Pro this was fine unless I included #pragma glsl in which case hyperbolic functions would report an error of "no matching overloaded function found". But now under 5.0 on OpenGL shaders are always compiled into GLSL even without the pragma. To quote the update notes:

    "On desktop OpenGL, shaders are always compiled into GLSL instead of ARB_vertex/fragment_program target now. You no longer need to write "#pragma glsl" to get to features like vertex textures or derivative instructions. All support for ARB_vertex/fragment_programs is removed."

    I can see that the shaders are still compiling for D3D but not for OpenGL or OpenGL ES. How do I go about getting hyperbolic functions to compile into glsl in Unity?

    Update: I tried a messy workaround by providing a separate native GLSL subshader to avoid the conversion from CG but this resulted in the error: "Invalid call of undeclared identifier 'tanh'". Thinking this might be due to the default version number being too low I tried setting version within the subshader but got an error message that #version needed to be at the top of the program even though it was at the top of the subshader. So, still no way to get hyperbolic shader functions for OpenGL in Unity 5.0.
     
    Last edited: Apr 17, 2015
  2. RunSwimFly

    RunSwimFly

    Joined:
    Mar 2, 2011
    Posts:
    34
    I wan't able to get this to work. Fortunately tanh can be expressed in terms of the exponential function so I now call this function in place of tanh:
    Code (CSharp):
    1. inline half4 Tanh(half4 vec)
    2. {
    3. #ifdef SHADER_TARGET_GLSL
    4.     half4 eVec = exp(vec*-2.0);
    5.     return (1.0-eVec)/(1.0+eVec);
    6. #else
    7.     return tanh(vec);
    8. #endif
    9. }
    I'm seeing a 15% drop in performance over 4.6 but I doubt it's due to this. And it's still not working properly. I've got another laptop with 4.6 on it so I'll use that to flip my shaders one by one to glsl and hopefully find the culprit(s).
     
    Last edited: Apr 18, 2015