john
john

Reputation: 57

OpenGL ES God Ray Precision error

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

enter image description here

enter image description here

My source of inspiration was: http://fabiensanglard.net/lightScattering/

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.

http://community.arm.com/thread/4024

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.

Upvotes: 0

Views: 354

Answers (2)

john
john

Reputation: 57

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: http://community.arm.com/message/29658?et=watches.email.thread#29658

For optimizations used see this post: http://www.gamedev.net/topic/670346-opengl-es-god-ray-precision-error/

Upvotes: 0

solidpixel
solidpixel

Reputation: 12239

One of the common differences is the use of mediump on mobile (which is commonly fp16) vs desktop (which often does not implement fp16, and just promotes to fp32).

If you use highp does it look any better?

Upvotes: 1

Related Questions