4

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);
 }
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Dima
  • 1,189
  • 1
  • 13
  • 31
  • The `sin()` implementation can be system specific. Look at [this question's answer](http://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl). To get betted idea on how your noise/random function works. – plastique Dec 23 '15 at 09:44

1 Answers1

3

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

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
Columbo
  • 6,648
  • 4
  • 19
  • 30
  • I've changed to highp and it works. It's really help, thank you. I steel see some contrast, but according to http://www.youi.tv/mobile-gpu-floating-point-accuracy-variances/ I won't be able to get rid off it. – Dima Dec 23 '15 at 09:59
  • Hi @Columbo, do you idea about my android apps? it's noising screen also but build using Unity+Android Studio - please see https://stackoverflow.com/questions/61571759/how-to-solve-noise-screen-error-in-android-like-noise-in-tv – questionasker May 03 '20 at 13:46
  • @coderInrRain no. This issue was to do with someone who was having difficulties trying to generate noise in a shader (it's often useful in special effects). Your question is about a rendering bug in your Unity scene which looks like noise - a completely different situation. – Columbo May 03 '20 at 20:07