Skip to content

Commit

Permalink
Fix glDrawElements emulation with FULL_ES2 (#22294)
Browse files Browse the repository at this point in the history
This patch fixes an issue related to defining the buffer size before
drawing. It introduces a slight overhead of O(buffersize), but is
probably ok in practice, and is necessary for correctness. (Also,
this only affects FULL_ES2, which is not on by default.)
  • Loading branch information
eukarpov authored Aug 26, 2024
1 parent c66e8cf commit cea6b82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3803,19 +3803,47 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
glDrawElements: (mode, count, type, indices) => {
#if FULL_ES2
var buf;
var vertexes = 0;
if (!GLctx.currentElementArrayBufferBinding) {
var size = GL.calcBufLength(1, type, 0, count);
buf = GL.getTempIndexBuffer(size);
GLctx.bindBuffer(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, buf);
GLctx.bufferSubData(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/,
0,
HEAPU8.subarray(indices, indices + size));

// Calculating vertex count if shader's attribute data is on client side
if (count > 0) {
for (var i = 0; i < GL.currentContext.maxVertexAttribs; ++i) {
var cb = GL.currentContext.clientBuffers[i];
if (cb.clientside && cb.enabled) {
let arrayClass;
switch(type) {
case 0x1401 /* GL_UNSIGNED_BYTE */: arrayClass = Uint8Array; break;
case 0x1403 /* GL_UNSIGNED_SHORT */: arrayClass = Uint16Array; break;
#if FULL_ES3
case 0x1405 /* GL_UNSIGNED_INT */: arrayClass = Uint32Array; break;
#endif
default:
GL.recordError(0x502 /* GL_INVALID_OPERATION */);
#if GL_ASSERTIONS
err('type is not supported in glDrawElements');
#endif
return;
}

vertexes = new arrayClass(HEAPU8.buffer, indices, count).reduce((max, current) => Math.max(max, current)) + 1;
break;
}
}
}

// the index is now 0
indices = 0;
}

// bind any client-side buffers
GL.preDrawHandleClientVertexAttribBindings(count);
GL.preDrawHandleClientVertexAttribBindings(vertexes);
#endif

GLctx.drawElements(mode, count, type, indices);
Expand Down
9 changes: 8 additions & 1 deletion test/third_party/cubegeom/cubegeom_pre_vao_es.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,19 @@ int main(int argc, char *argv[])
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36);

glBindVertexArray(0);

glDeleteVertexArrays(1, &vao);

// Validate glDrawElements when a client side vertex array is used.
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, arrayData);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, arrayData + 16);
glEnableVertexAttribArray(1);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, elementData + 18);

// END

SDL_GL_SwapBuffers();
Expand Down

0 comments on commit cea6b82

Please sign in to comment.