GLSL & GLES Small Difference

Issue #1367 new
Matías Hermosilla
created an issue

Hello,

trying a library I made, that I tested on android didn't seem to do what I expected it to do, it seemed that shader effect had a issue.

This is how it works on PC (GLSL) PC.png

And how it works on Android (GLES) Android.png

As you can see the fade-effect from the light isn't working well on larger distances, it makes a square around the light. That's because the formula I was using divides the distance of the pixel to the center of the light, therefore there's a loss of precision in that calculation.

vec4 effect(vec4 Color, Image Texture, vec2 tc, vec2 pc) {

        float Distance = distance( vec3(pc, 0.0), Center);

        if (Distance <= Radius) {

            return mix(vec4(0.0), vec4(1.0), 1.0 - Distance / Radius);

        }

        return vec4(0.0, 0.0, 0.0, 0.0);
    }

My quick solution was to add this to the shader header.

#ifdef GL_ES
    #ifdef GL_FRAGMENT_PRECISION_HIGH
        precision highp float;
    #else
        precision mediump float;
    #endif
#endif

Which immediately solved the problem, now it doesn't break on android.

I would propose to add this to the shader headers, that are added to every shader created via newShader, but it is not my duty to evaluate what the cost of using this is going to be (if it's going to cause a bigger overhead, etc).

So it's up to you (developers), this is just an enhancement I'm proposing.

Comments (4)

  1. Alex Szpakowski

    Unfortunately not every OpenGL ES device that love runs on supports highp in pixel shaders, and some of the old devices that do support it will have fairly significant slowdowns if it's used everywhere.

    You can mark your distance-related variables with highp if you don't care about those really low end/old devices.

  2. Alex Szpakowski

    Another option might be to do the distance calculation in a vertex shader (which does always support and use highp), and pass the normalized result down to the pixel shader, it'll be interpolated across the pixels of the objects you render.

  3. Log in to comment