OpenGL ES God Ray Precision error

| | August 4, 2015

I have encountered the following (i think) precision error.

enter image description here

(missing link need 10 rep)

My source of inspiration was:

(missing link need 10 rep)

On the PC everything works fine, but on android it shows those weird squares.

I had the same problem with a procedurally masked sprite. When the radius of the circle was getting too big i had the same error so i changed the mask from a shader radius uniform to a texture mask uniform, so i guess there is a precision problem.

This guy here had the same problem but unfortunately i can’t see the answer.

(missing link need 10 rep please someone upvote me.)

The code adapted to OpenGL ES is the following:

#version 100

precision mediump float;

uniform sampler2D tex_diff;
uniform vec2 light_on_screen;

varying vec2 texture_coord;

const int NUM_SAMPLES = 128;

void main()
{
    const float exposure = 0.0225;
    const float decay = 0.95;
    const float density = 0.95;
    const float weight = 3.75;

    // Inner used valuesa
    vec2 deltaTextCoord = vec2(texture_coord.st - light_on_screen.xy);
    vec2 textCoo = texture_coord.st;
    deltaTextCoord *= 1.0 / float(NUM_SAMPLES) * density;
    float illuminationDecay = 1.0;

    vec4 c = vec4(0, 0, 0, 0);

    for(int i=0; i < NUM_SAMPLES ; i++)
    {
        textCoo -= deltaTextCoord;

        textCoo.s = clamp(textCoo.s, 0.0, 1.0);
        textCoo.t = clamp(textCoo.t, 0.0, 1.0);

        vec4 sample = texture2D(tex_diff, textCoo);

        sample *= illuminationDecay * weight;

        c += sample;

        illuminationDecay *= decay;
    }

    c *= exposure;

    c.r = clamp(c.r, 0.0, 1.0);
    c.g = clamp(c.g, 0.0, 1.0);
    c.b = clamp(c.b, 0.0, 1.0);
    c.a = clamp(c.a, 0.0, 1.0);

    gl_FragColor = c;
}

Showing the whole engine is useless since it’s huge. All the shader input is correct, the coordinates are correct, the only problem is the inner shader computation.

Anybody encountered this or has any ideas for any workarounds?

Scanned the whole net for a solution for this, and i couldn’t seem to find one. Any can point me in the right direction? Or maybe someone encountered this type of error in a different context or in a different shader? Maybe i can apply the same workaround here too.

One Response to “OpenGL ES God Ray Precision error”

  1. Updated with a minor fix:

    enter image description here

    enter image description here

    Yes, the problem was:

    textCoo -= deltaTextCoord;
    

    Due to the fp precision the error accumulated grows to something like 2.7%. Calculating it each loop instead of subtracting a delta fixes a big part of the problem.

    As for optimization, I am using some techniques, got a reasonable 40fps.

    See this post, I asked the same question here.

    For optimizations used see this post about god ray precision error.

Leave a Reply