Dima
Dima

Reputation: 1189

Noise screen on OpenGLES Android

I try to create noise screen on Android device. For getting random I use this formula:

fract((sin(dot(gl_FragCoord.xy ,vec2(0.9898,78.233)))) * 4375.85453)

I tested it in http://glslsandbox.com/ and it's work perfect.

Result form http://glslsandbox.com/

enter image description here

But on phone and tablet I have another result

On Nexus 9 I receive this:

enter image description here

But it's not so bad. On LG-D415 I receive this

enter image description here

Can somebody help me with it?

Shader:

 #ifdef GL_ES
 precision mediump float;
 #endif

 float rand(vec2 co){
            return fract((sin(dot(co.xy ,vec2(0.9898,78.233)))) * 4375.85453);
 }
 void main() {
    vec2 st = gl_FragCoord.xy;
    float rnd = rand(st);
    gl_FragColor = vec4(vec3(rnd),1.0);
 }

Upvotes: 4

Views: 851

Answers (1)

Columbo
Columbo

Reputation: 6766

It's almost certainly a precision issue. Your mediump based code might be executed as standard single precision float on some devices or half precision on others (or anywhere in between).

Half precision simply isn't enough to do those calculations with any degree of accuracy.

You could change your calculations to use highp, but then you'll run into the next problem which is that many older devices don't support highp in fragment shaders.

I'd strongly recommend you create a noise texture, and just sample from it. It'll produce much more consistent results across devices, and I'd expect it to be much faster on the majority of mobile GPUs.

This is an interesting article about floating point precision: http://www.youi.tv/mobile-gpu-floating-point-accuracy-variances/

This is the related app on the Google Play store.

This is a more in-depth discussion of GPU precision: https://community.arm.com/groups/arm-mali-graphics/blog/2013/05/29/benchmarking-floating-point-precision-in-mobile-gpus

Upvotes: 3

Related Questions