Problem with defining float precision in shader

I want to use highp precision for float in shader in web. But if I set the following code:

#ifdef GL_ES
precision highp float;
#endif

varying mediump vec2 var_texcoord0;
varying lowp vec4 var_color;

Then the following error appears:
/materials/gui_gradient.fp _generated_b7060373.fpc:\Users\user\AppData\Local\Temp_generated_b7060373.fpc650248135771408284.glsl ERROR: C:\Users\user\AppData\Local\Temp_generated_b7 060373.fpc650248135771408284.glsl:23: ‘_DMENGINE_GENERATED_gl_FragColor_0’ : undeclared identifier ERROR: C:\Users\user\AppData\Local\Temp_generated_b7060373.fpc650248135771408284.glsl:23: ‘’ : compilation terminated
ERROR: 2 compilation errors. No code generated.

SPIR-V is not generated for failed compile or link

I have to release the code below:

varying mediump vec2 var_texcoord0;
varying lowp vec4 var_color;

#ifdef GL_ES
precision highp float;
#endif

And then the project compiles. But when I run it, I get the following error:
Error: ERROR: 0:3: '' : No precision specified for (float)

I couldn’t overcome this error. What am I doing wrong?

full shader code:

varying mediump vec2 var_texcoord0;
varying lowp vec4 var_color;

#ifdef GL_ES
    precision highp float;
#endif

uniform lowp vec4 color_1;
uniform lowp vec4 color_2;

uniform lowp sampler2D texture_sampler;

void main()
{
    vec4 color = mix(color_2, color_1, smoothstep(0.0, 1.0, var_texcoord0.y));

    gl_FragColor = color * var_color;
}

I decided to run the modified gui.fp shader on an empty project and see the generated code.
gui.fp:

#ifdef GL_ES
precision highp float;
#endif

varying mediump vec2 var_texcoord0;
varying lowp vec4 var_color;

uniform lowp sampler2D texture_sampler;

void main()
{
    lowp vec4 tex = texture2D(texture_sampler, var_texcoord0.xy);
    gl_FragColor = tex * var_color;
}

Build error:

/main/gui.fp
_generated_b30b8ec.fpc:\Users\user\AppData\Local\Temp_generated_b30b8ec.fpc6468856637887905037.glsl
ERROR: C:\Users\user\AppData\Local\Temp_generated_b30b8ec.fpc6468856637887905037.glsl:16: ‘_DMENGINE_GENERATED_gl_FragColor_0’ : undeclared identifier
ERROR: C:\Users\user\AppData\Local\Temp_generated_b30b8ec.fpc6468856637887905037.glsl:16: ‘’ : compilation terminated
ERROR: 2 compilation errors. No code generated.

SPIR-V is not generated for failed compile or link

As a result, the following code is generated in the _generated_dd5cf069.fpc12950698198763364097.glsl file:

#version 140
#ifdef GL_ES

out vec4 _DMENGINE_GENERATED_gl_FragColor_0;
precision highp float;
#endif

in mediump vec2 var_texcoord0;
in lowp vec4 var_color;

layout(set=1) uniform lowp sampler2D texture_sampler;

void main()
{
    lowp vec4 tex = texture(texture_sampler, var_texcoord0.xy);
    _DMENGINE_GENERATED_gl_FragColor_0 = tex * var_color;
}

For some reason, the code out vec4 _DMENGINE_GENERATED_gl_FragColor_0; consists inside #ifdef GL_ES.
What am I doing wrong? :sweat_smile:

On mobile and web platforms, Defold sets the default precision by itself. On desktop, there is no precision option at all.

All you can do is to set individual precision for specific variable (as you did).

Plus, vote for the issue - Option to set GLSL ES precision for the whole shader · Issue #6519 · defold/defold · GitHub

1 Like

Thank you very much for the clarification! As luck would have it, this problem has already been fixed in version 1.9.2 :grinning_face_with_smiling_eyes: