I too faced the problem of dynamic loop iteration count the other day, and found that you can use a hard-coded max in the for-loop condition, then put an if-statement inside the loop that breaks the loop when reaching the "dynamic max" iteration count, e.g. passed as
a uniform. There are probably caveats, but it works.
Yeah, that's a good solution, thanks! I think one caveat is that on some devices conditional WebGL code will execute even if it isn't run, so you probably shouldn't set the initial ceiling absurdly high, although it's probably safe if passed as a uniform.
I have the additional problem here is that I'm too stupid/lazy to think of this as an ascending loop rather than a descending one, so it's only the last iterations of the loop that are guaranteed to run.