Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Set GLSL version from CG shader?

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

  1. RunSwimFly

    RunSwimFly

    Joined:
    Mar 2, 2011
    Posts:
    34
    Is there any way to set the GLSL version number from a CG shader? It seems that the GLSL produced is using a very low version number. Now that GLSL is forced for OpenGL under Unity 5.0 this is causing me problems in a number of areas, particularly returning arrays from functions. I've seen the topic raised as a side issue in several discussions but I can't see that it's ever been addressed.
     
  2. RunSwimFly

    RunSwimFly

    Joined:
    Mar 2, 2011
    Posts:
    34
    So, just for the record, here’s what I had to do to get all my shaders to work under 5.0 OpenGL. (all of which will be redundant if someone tells me how to get Unity to compile with a higher GLSL version.)

    1. Eliminate all instances of “out” array parameters.

    2. Loops with sampler arrays had to be unrolled to get the GLSL shader to compile. But then, even though the shader compiled, sampler arrays still produced incorrect results. (Finding that these were the culprit with no error messages was the most time consuming part of the conversion.). So now I’m using unrolled loops with a bunch of discrete sampler variables. It’s not pretty.

    3. There's no tanh function. Replaced with a formula using the exponential function which is available.

    On the plus side the performance appears to be about the same but the code is larger and less maintainable. If you haven't updated to 5.0 yet, then go through your shaders and drop #pragma glsl into each one by one first. It's implied and forced under 5.0 and it's much easier to make the change in a stepwise function to a 4.x version before updating.
     
  3. UnityGuillaume

    UnityGuillaume

    Unity Technologies

    Joined:
    Mar 16, 2015
    Posts:
    123
    The "pragma version" directive usually select corresponding GLSL version (target 3.0 generate version 150, target 4.0 version 330 etc...) with a few extension I think.
    At least that's what you see when you open compiled shader (in 5.x, with a shader selected, you have a button "Compile and show code". You can click on the little arrow to deselect all platform but OpenGL one, to only have OpenGL shader generated. Then click on the button and you'll see generated shader).


    I don't think you can set the version by hand though...except if you write pure glsl but then your shader only work on GL capable plateform...
     
    RunSwimFly likes this.
  4. RunSwimFly

    RunSwimFly

    Joined:
    Mar 2, 2011
    Posts:
    34
    Thanks for the reply. Target 4.0 isn't supported on my latest gen Macbook Pro so would probably miss much of the market. I did try writing a separate GLSL subshader when I first hit this problem but found that trying to change version evoked an error saying that #version had to be the first instruction. I didn't check but assumed that Unity was inserting some instructions.
    Anyway, with the changes listed above I've got it running now and then once I changed a couple of ARGBFloat render textures to ARGBHalf it ran under iOS as well. I'd only bought the iPad Air 2 I needed to run it the day before. This is my second Unity project (I haven't finished the first just yet) and it's still a thing of wonder (and no small relief) to me that I can give no regard to a completely different architecture and then have it running on that platform within a day or two. Nice work Unity!