V4.0 glsl-optimizer for Metal and some other issues

V4.0 glsl-optimizer for Metal and some other issues


I wanted to reply to this thread but unfortunately it’s closed :frowning:

Anw, thank you cocos2d-x team for bringing Metal support in v4.0! I have been using it and it works fine out of the box.
However, i have bump into an issue with my custom shaders. For example in this code snippet.

void main()
    v_fragmentColor = a_color;
    v_texCoord = a_texCoord;

    vec2 flowDir = vec2(texture2D(u_flowTexture, a_texCoord).rg);
    vec4 pos = a_position;
    pos.xy -= flowDir * u_time;
    gl_Position = u_MVPMatrix * pos;

After going through the glsl-optimiser, the vec2 flowDir is swapped with a type of half4 instead of float4. And since gl_Position underwent some operation with vec4 pos, which underwent an operation with flowDir, this resulted in gl_Position being declared as a half4 and not float4 and this will cause the Metal shader to fail to compile because vertex shader has to return a float4 gl_Position.

Basically, if the gl_Position is calculated from anything that derives from a variable that derives from a half, it will have the precision reduced to half instead of float. And I notice that anything that results from texture2D operation always result in a half.

Has anybody came across this issue and found a solution for it?

I had a temporary fix for this on a leaner project, whereby I have a separate file for the Metal shader that I manually corrected the variable type. But I had to perform some edit to the engine’s renderer codes so that it takes in a separate file.
But I don’t think this is a viable solution when it comes to a big project. The ideal case is to manage just 1 file for each shader instead of having to manage different shader language versions of it.