[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] some samples and a question about looping

The WebGL spec requires that GLSL implementations limit themselves to
the MINIMUM OpenGLES2.0 requirements - so we don't allow loops with a
non-constant number of iterations (and we specifically disallow those
where the value of a uniform is used to determine the termination

In other words, only loops that could (in principle) be unrolled at
compile time are allowed.

I believe that there are two reasons for this - one is that a lot of
hardware doesn't support loops at all - so the compiler is forced to
unroll them.  The other is as a kind of security measure to make sure
the bad guy can't lock up your computer by putting a shader into an
infinite loop.

Now - I agree that there is an issue here...while it's reasonable to
require loops that could in principle be unrolled - should WebGL force
them to actually be unrolled in practice?   Arguably, it shouldn't
because that makes the shader code much longer than it needs to be on
machines that CAN support looping at the hardware level.

I say "arguably" because the current restrictions don't seem to prevent
you from saying something like:

     for ( float i = 100000000 ; i < 100000001 ; i += 0.1 )
       dosomething ;

...which is legal (per the specification) - and on paper, only iterates
10 times.   But in practice, it would probably loop forever because math
precision isn't good enough and 100000000+0.1 evaluates to 100000000, so
the loop never reaches its target.  If loops are not literally unrolled
by the compiler, this kind of infinite looping would be hard to
definitively exclude.

You might imagine that the shader verifier could try unrolling the loop
to make sure it's possible - and if that works, pass on the original
rolled-up loop to the underlying GLSL compiler - but that's awfully
risky if you don't know the precise inner workings of the GPU's floating
point math unit.  The bad guy might easily find a 'chink' in that armor
with carefully-chosen data.

Your work-around of looping a very large number of times and then
breaking out in the middle is legal in principle - but if the compiler
does unroll loops, you'll run out of shader instructions and it won't

  -- Steve

On 12/24/2010 10:51 AM, Sami Mäkelä wrote:
> Hello!
> I've updated my samples at http://programming-progress.com/webgl.html
> so that they now work (or do not work) as expected on the latest
> browsers. At least check out the Xmas star.
> One question: It seems like the ATI GL drivers on at least Windows 7
> 64bit seem to always unfold loops that have constant number of
> iterations. My workaround was to force the looping to be unknown (by
> using uniforms). This doesn't work anymore because ANGLE validator
> doesn't accept it. Is there some example on any platform where
> converting the shader code to the form accepted by ANGLE would
> actually convert a non-working shader to a shader that works with the
> driver?
> Anyway even if there is some driver that cannot handle full looping,
> it should not matter, because any program can be converted into the
> form accepted by ANGLE for example using
> while (cond) body; ===> for (int i = 0; i < INFINITY-1; i++) { if
> (!(cond)) break; else body; }
> As summary, this check might cause some shaders to become impossible
> on some environments, and it does not add to compatibility, because
> the shaders could be easily converted into form that should be
> accepted, if the original code is rejected by the driver. Wouldn't it
> just be better to drop this check and say that the loops are "too
> long" (and hence undefined) if they are not supported by the driver?
> -- Sami
> -----------------------------------------------------------
> You are currently subscribed to public_webgl@khronos.org.
> To unsubscribe, send an email to majordomo@khronos.org with
> the following command in the body of your email:

You are currently subscribed to public_webgl@khronos.org.
To unsubscribe, send an email to majordomo@khronos.org with
the following command in the body of your email: