Skip to content

Commit d9ac1ee

Browse files
authored
Allow glBindBuffer with an unknown buffer (#23525)
Apparently the correct behavior is just to create one. Fixes: #18886
1 parent 2f593a8 commit d9ac1ee

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/lib/libwebgl.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,13 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
348348
for (var i = table.length; i < ret; i++) {
349349
table[i] = null;
350350
}
351+
#if FULL_ES2
352+
// Skip over any non-null elements that might have been created by
353+
// glBindBuffer.
354+
while (table[ret]) {
355+
ret = GL.counter++;
356+
}
357+
#endif
351358
return ret;
352359
},
353360

@@ -2971,6 +2978,16 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
29712978
},
29722979

29732980
glBindBuffer: (target, buffer) => {
2981+
#if FULL_ES2
2982+
// Calling glBindBuffer with an unknown buffer will implicitly create a
2983+
// new one. Here we bypass `GL.counter` and directly using the ID passed
2984+
// in.
2985+
if (buffer && !GL.buffers[buffer]) {
2986+
var b = GLctx.createBuffer();
2987+
b.name = buffer;
2988+
GL.buffers[buffer] = b;
2989+
}
2990+
#endif
29742991
#if GL_ASSERTIONS
29752992
GL.validateGLObjectID(GL.buffers, buffer, 'glBindBuffer', 'buffer');
29762993
#endif

test/browser/webgl_create_context.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,14 @@ int main() {
155155
printf("\n");
156156

157157
// Test bug https://github.com/emscripten-core/emscripten/issues/1330:
158+
159+
#if FULL_ES2
160+
// Test bug https://github.com/emscripten-core/emscripten/issues/18886
161+
unsigned vb = 100;
162+
#else
158163
unsigned vb;
159164
glGenBuffers(1, &vb);
165+
#endif
160166
glBindBuffer(GL_ARRAY_BUFFER, vb);
161167
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
162168

@@ -180,6 +186,9 @@ int main() {
180186
glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled);
181187
assert(enabled == 0);
182188

189+
#if FULL_ES2
190+
glDeleteBuffers(1, &vb);
191+
#endif
183192
// Test that deleting the context works.
184193
res = emscripten_webgl_destroy_context(context);
185194
assert(res == 0);

test/test_browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,7 @@ def test_html5_webgl_create_context_no_antialias(self, args):
26352635
@parameterized({
26362636
'': ([],),
26372637
'closure': (['-O2', '-g1', '--closure=1'],),
2638-
'full_es2': (['-sFULL_ES2'],),
2638+
'full_es2': (['-sFULL_ES2', '-DFULL_ES2', '-sGL_ASSERTIONS'],),
26392639
'pthread': (['-pthread'],),
26402640
})
26412641
def test_html5_webgl_create_context(self, args):

0 commit comments

Comments
 (0)