[Public WebGL] Index Validation and using the same buffer for both GL_ARRAY_BUFFER and GL_ELEMENT_ARRAY_BUFFER

Chris Marrin [email protected]
Wed Jan 13 14:50:55 PST 2010

On Jan 13, 2010, at 11:32 AM, Kenneth Russell wrote:

> On Tue, Jan 12, 2010 at 4:36 PM, Chris Marrin <[email protected]> wrote:
> On Jan 12, 2010, at 3:23 PM, Gregg Tavares wrote:
> > I was in the process of writing some index validation code before calling glDrawElements and I found out that apparently some WebGL implementations currently assume that the same buffer will never be used for both a GL_ARARY_BUFFER and a GL_ELEMENT_ARRAY_BUFFER.
> >
> > as in
> >
> >     buf = ctx.createBuffer();
> >     ctx.bindBuffer(ctx.ARRAY_BUFFER, buf);
> >     ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, buf);
> >
> > The spec does not disallow this.
> >
> > My understanding is the reason some implementations assumed this would never happen was that to efficiently validate the indices, client side shadow copies of the buffer data needed to be kept. If there is an assumption that any buffer bound to GL_ELEMENT_ARRAY_BUFFER will only ever be bound to GL_ELEMENT_ARRAY_BUFFER then only the few buffers that are bound to GL_ELEMENT_ARRAY_BUFFER need to be shadowed.
> I will assume you're talking about the WebKit implementation. When I did that, I tried not to make an assumption that they would be different buffers. It tried to keep information about both types separate so a single buffer could represent both. If you use the same buffer for both you'd be shadowing much more than you needed to, but I think it should still work. Let me know what the case is where this will fail if the same buffer is used for both and we can see if it can be fixed.
> The case that will fail is:
>   - Bind the buffer to ARRAY_BUFFER
>   - Use bufferData to upload unsigned byte or short data which will be treated as indices
>   - Unbind the buffer
>   - Bind the buffer to ELEMENT_ARRAY_BUFFER
>   - Call drawElements, referring to the previously uploaded data as indices
> Because the buffer was bound to ARRAY_BUFFER when the data was uploaded, the WebKit index validation code won't keep a client-side copy.

I'm not sure what you've just done is valid. I believe (and my implementation reflects this) that ARRAY_BUFFER and ELEMENT_ARRAY_BUFFER are two separate bind points and therefore constitute different buffer objects. For instance, if you do what you propose above, the ELEMENT_ARRAY_BUFFER you've bound would contain no data. Why else would you pass target to bufferData()?

> Possible solutions include:
>   1. Keep a client-side copy of *all* data uploaded to buffer objects. Very wasteful of memory.
>   2. Lazily pull back data from buffer objects bound to ELEMENT_ARRAY_BUFFER using glMapBuffer when they are referenced from drawElements. Once client-side data exists for a buffer object, always keep it in sync during bufferData and bufferSubData calls.
>   3. Forbid binding the same buffer object to the ARRAY_BUFFER and ELEMENT_ARRAY_BUFFER points in the WebGL spec.
> We should keep in mind that hardware and drivers are moving in the direction where index clamping will be done in hardware.
> I'm not sure whether solution (3) would impact any real-world apps; I haven't seen any demos which would be affected.
> Personally I would prefer (3), with (2) as an alternative, but not (1).

First we need to understand if there is an issue. I don't have time to do a simple experiment and I can't find any relevant sections in the spec right now. But I'm not sure there is an issue here.

[email protected]

You are currently subscribe to [email protected]
To unsubscribe, send an email to [email protected] with
the following command in the body of your email:

More information about the public_webgl mailing list