Skip to content

Assert when glDrawElements cannot draw anything because of missing vertex bounds#26525

Merged
sbc100 merged 1 commit intoemscripten-core:mainfrom
paradust7:add_ibo_assert
Mar 31, 2026
Merged

Assert when glDrawElements cannot draw anything because of missing vertex bounds#26525
sbc100 merged 1 commit intoemscripten-core:mainfrom
paradust7:add_ibo_assert

Conversation

@paradust7
Copy link
Copy Markdown
Contributor

@paradust7 paradust7 commented Mar 23, 2026

Even with FULL_ES2, glDrawElements cannot handle the case where the indices are in a buffer but the vertices are client-side. Nothing will be drawn in this case. Add an assertion to make this situation visible when debugging.

Fixes #26460

if (!cb.clientside || !cb.enabled) continue;

#if ASSERTIONS
assert(count || !GLctx.currentElementArrayBufferBinding, 'must use array buffers when using element buffer');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this assert belong here inside this for loop? Could it not go outside the loop?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be !count || !GLctx.currentElementArrayBufferBinding ? (i.e. I'm not sure what the count || part is doing here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is written as intended, but there may be an edge case I didn't consider.

preDrawHandleClientVertexAttribBindings is called in two places. It is called by glDrawArrays, where the vertex count is always passed by the user. And in glDrawElements, where the vertex count is computed if it can be, or otherwise passed as 0.

The error case is only for glDrawElements, when the vertex count cannot be computed, so that the count is passed as 0, even though the real count is non-zero.

I added the "count ||" to make sure the assert doesn't fire for glDrawArrays. But it could still fire if glDrawArrays is called with a count of 0.

I'll get a fix for this ready.

Copy link
Copy Markdown
Collaborator

@juj juj Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pondering on this a bit, it does seem that

assert(count == 0 || !GLctx.currentElementArrayBufferBinding, 'must use array buffers when using element buffer');

would work well.

With

assert(count || !GLctx.currentElementArrayBufferBinding, 'must use array buffers when using element buffer');

the assert would only ever fire if count == 0 && index buffer bound (i.e. we are not drawing anything and there is an index buffer bound)

The assert logic in general that verifies that there cannot be a GPU-side index buffer bound when the rendering has any CPU-side vertex attribute pointers seems sound. (we'd have to track the min/max vertex index at buffer upload time, like browsers internally also do).

Does this assert belong here inside this for loop?

Yeah, it must be inside the loop, since the assert only applies if there is at least one vertex attribute stream found that has a client-side buffer pointer (i.e. attribute stream index for which cb.clientside == true)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@juj The original issue explains the case I'm trying to fix:

#26460

The problem case occurs when the element buffer is not client-side, but one or more array buffers is. In this situation, glDrawElements always passes count=0 to preDrawHandleClientVertexAttribBindings, because it doesn't know the real vertex count. This results in nothing being drawn.

I think it would be cleaner to pass count=null when the vertex bounds are unknown, and check for null explicitly in the loop. The only downside of this is that the null check has to occur even when assertions are disabled, but the check is pretty cheap.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this situation, glDrawElements always passes count=0 to preDrawHandleClientVertexAttribBindings, because it doesn't know the real vertex count. This results in nothing being drawn.

Ohh, gotcha. Looks good to me then. Might be good to drop this note as a comment before the assert?

@sbc100
Copy link
Copy Markdown
Collaborator

sbc100 commented Mar 25, 2026

I'm afraid I don't really know this code well enough to know for sure if this correct. @juj WDYT?

@sbc100 sbc100 merged commit 0dc08a3 into emscripten-core:main Mar 31, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

glDrawElements draws nothing when ELEMENT_ARRAY_BUFFER is bound, but ARRAY_BUFFER is not.

3 participants