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

Re: [Public WebGL] Consumed Attribute



What Steve said.

It's for the case of you turn on 3 attributes with 3 vertices each (drawing a triangle with a shader that uses positions, texcoords and normals). You call drawArrays with a count of 3.  Next you draw 2 triangles, 6 vertices and use a shader that only uses positions. You call gl.vertexAttribPointer to setup your position vertex attrib to point to 6 position values but there are still 2 other vertex attribs setup from the previous draw call each with only 3 vertices available. You call drawArrays with a count of 6.

Since WebGL enforces that you don't try to access out of bounds values you have 2 options

1) fail the draw. You asked to draw 6 but 2 of your attribs only reference buffers that have 3 vertices each

2) You pass the draw because you know the shader is not going to access those 2 other vertex attribs.

The spec says do #2.


On Fri, Sep 17, 2010 at 4:40 AM, Steve Baker <steve@sjbaker.org> wrote:
I'm not sure about an exact definition - but what it means is that if
(for example) you send a TexCoord2 value to your vertex shader - but it
doesn't use it for anything, or perhaps doesn't even declare it - that's
not an error, the extra data is silently ignored.

There are at least two reasons why that's a good idea:

1) Shader optimization sometimes means that a variable that you declared
and perhaps even used in the shader gets optimized away by the
compiler.  You don't want that to cause an error because it's not
obvious to the naive programmer what went wrong or how to fix it.

  eg:

        x = TexCoord2.x * 0.0000001 ;

....on a machine with full 32 bit floating point, this compiles to
actual working code - but on a machine with only 16 bit half-float, that
constant gets truncated to zero - the compiler spots a multiplication by
zero and replaces that line with:

       x = 0.0 ;

...and the TexCoord2 value is now "unused" - and in at least some shader
compilers will not appear in the list of attributes if you query them
from the application.

2) Sometimes you want to send the same set of vertex attributes to a
number of different shaders (eg if you have a depth-only pass, a shadow
pass and a beauty pass) - some of which need only a subset of the
attributes.  eg When you use an interleaved vertex data format, it could
be inconvenient to have to send only exactly what the shader wanted.

In general, it's very convenient for it not to be illegal to try to pass
things to a shader that it doesn't need.

 -- Steve


Erik Möller wrote:
> From WebGL spec 6.2:
>
> "If a vertex attribute is enabled as an array, a buffer is bound to
> that attribute, but the attribute is not consumed by the current
> program, then regardless of the size of the bound buffer, it will not
> cause any error to be generated during a call to drawArrays or
> drawElements."
>
> What exactly is the definition of an attribute being consumed by a
> program?
>

-----------------------------------------------------------
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: