0

I have a thorny issue I'm trying to solve inside a webgl GLSL fragment shader. For context, I'm implementing a flocking simulation using a ping-pong technique. Everything works properly on PC & Mac, but is buggy on IOS (in Safari/Chrome/Firefox). There is no error, just behavior that is incorrect.

I've tracked it down to a place where I'm using a vec4 that is sampled from a texture, and that value seems corrupt in some way. If I do not use the sampled value, the results are similar on IOS as other devices.

I believe the sampled value has a corrupt, or otherwise unexpected value because of this behavior:

vec4 sampled = texture2D( inputTexture, textureCoordinate);

// a condition that shouldn't fire
// except perhaps for a 'special value' -
// (e.g. something like nil, null, NaN, undefined)
if (sampled.x > 0.0 && sampled.x <= 0.0) {
    // do something that modifies behavior
    // THIS FIRES! WHY!
}

Anyone have any clues that might point me in the right direction? Is there a good way to debug values within fragment shaders that might help?

bjnsn
  • 2,710
  • 2
  • 16
  • 14
  • Can you add logging, an alert, etc. inside that if to capture the value of sampled.x? – Dave S Apr 27 '19 at 00:13
  • Actually I've seen this with very little values of X, easiest way that looked like working is to add some integer to it, like +1 , and redo comparison. – Gar Apr 27 '19 at 00:45
  • maybe `NaN`s or `Inf`s – LJᛃ Apr 27 '19 at 19:48
  • Are you using `precision highp float`? – gman Apr 27 '19 at 22:02
  • @gman No. I'm actually specifically using [half float](https://developer.mozilla.org/en-US/docs/Web/API/OES_texture_half_float) because that resolved [other issues](https://github.com/mrdoob/three.js/issues/9628) with GPGPU techniques and IOS. – bjnsn Apr 29 '19 at 15:33
  • @dave-s I'm currently using the 'render something obvious to the screen' technique. The [tools & techniques](https://stackoverflow.com/questions/2508818/how-to-debug-a-glsl-shader) I've found are pretty limited, especially 1) in fragment shaders 2) in a feedback loop like this and 3) on IOS. I'm open to suggestions! – bjnsn Apr 29 '19 at 15:48
  • @Gar Interesting. I tried `if (sampled.x + 1. > 0.0 && samples.x + 1. <= 0.0) { /*...*/}` and the results still indicate the conditional is evaluating to true. – bjnsn Apr 29 '19 at 15:58
  • Why don't you post an [mcve](https://meta.stackoverflow.com/a/349790/128511) using a [snippet](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/). It should be pretty trivial to make a small sample that repos your issue. Note: I suspect that half float is just not enough resolution to do whatever it is you're trying to do. Note: At least Chrome iPhone you can view/run snippets by picking "Request Desktop Site" – gman Apr 29 '19 at 16:25

0 Answers0