Search code examples
openglopengl-eslibgdx

Detecting OpenGL ES in LibGDX to adjust shader code accordingly


I have a LibGDX app that runs on mobile and desktop systems just fine except for one tiny issue:

On mobile devices I need to include precision qualifiers in my shaders (precision highp float), but on Mac OS this leads to an error when I compile the shader since precision qualifiers technically only exist on OpenGL ES (although Windows just ignores them).

Is there a way in LibGDX to determine whether I am running under OpenGL ES or under regular OpenGL so I can adjust my shader code accordingly? Other than the obvious super-inelegant solution, of course: Try to compile the shader with the qualifiers, if it fails try again without.


Solution

  • Yes, you can use preprocessing directives at the top of your fragment shader.

    #ifdef GL_ES 
    precision highp float;
    #endif
    

    You can also use placeholders so you can adjust precision of specific variables.

    #ifdef GL_ES 
    #define LOWP lowp
    #define MEDP mediump
    #define HIGHP highp
    precision highp float;
    #else
    #define LOWP
    #define MEDP
    #define HIGHP
    #endif
    

    Then you can use the text LOWP in place of lowp and it will evaluate blank on desktop:

    varying LOWP vec4 v_color;