Skip to content

Commit

Permalink
Fix for zero count in glUniform family of functions
Browse files Browse the repository at this point in the history
The previous fix for this was in emscripten-core#16837, but I looks like that only
covered the new "garbage-free" webgl2 path.  When old webgl1 path was
still using the zero count value.

As part of this change I resurrected
`test_webgl_draw_triangle_with_uniform_color.c` which has not actually
be used since emscripten-core#20925.

Fixes: emscripten-core#21567
  • Loading branch information
sbc100 committed Mar 21, 2024
1 parent 2316d8a commit 5fd7fc0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ var LibraryGL = {
$miniTempWebGLFloatBuffers: [],
$miniTempWebGLFloatBuffers__postset: `var miniTempWebGLFloatBuffersStorage = new Float32Array({{{ GL_POOL_TEMP_BUFFERS_SIZE }}});
for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}; ++i) {
miniTempWebGLFloatBuffers[i] = miniTempWebGLFloatBuffersStorage.subarray(0, i+1);
miniTempWebGLFloatBuffers[i] = miniTempWebGLFloatBuffersStorage.subarray(0, i);
}`,

$miniTempWebGLIntBuffers: [],
$miniTempWebGLIntBuffers__postset: `var miniTempWebGLIntBuffersStorage = new Int32Array({{{ GL_POOL_TEMP_BUFFERS_SIZE }}});
for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}; ++i) {
miniTempWebGLIntBuffers[i] = miniTempWebGLIntBuffersStorage.subarray(0, i+1);
miniTempWebGLIntBuffers[i] = miniTempWebGLIntBuffersStorage.subarray(0, i);
}`,

$heapObjectForWebGLType: (type) => {
Expand Down Expand Up @@ -2439,7 +2439,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[count-1];
var view = miniTempWebGLIntBuffers[count];
for (var i = 0; i < count; ++i) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
}
Expand Down Expand Up @@ -2480,7 +2480,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 2 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[2*count-1];
var view = miniTempWebGLIntBuffers[2*count];
for (var i = 0; i < 2*count; i += 2) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'i32') }}};
Expand Down Expand Up @@ -2522,7 +2522,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 3 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[3*count-1];
var view = miniTempWebGLIntBuffers[3*count];
for (var i = 0; i < 3*count; i += 3) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'i32') }}};
Expand Down Expand Up @@ -2565,7 +2565,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 4 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[4*count-1];
var view = miniTempWebGLIntBuffers[4*count];
for (var i = 0; i < 4*count; i += 4) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'i32') }}};
Expand Down Expand Up @@ -2609,7 +2609,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[count-1];
var view = miniTempWebGLFloatBuffers[count];
for (var i = 0; i < count; ++i) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
}
Expand Down Expand Up @@ -2650,7 +2650,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 2 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[2*count-1];
var view = miniTempWebGLFloatBuffers[2*count];
for (var i = 0; i < 2*count; i += 2) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
Expand Down Expand Up @@ -2692,7 +2692,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 3 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[3*count-1];
var view = miniTempWebGLFloatBuffers[3*count];
for (var i = 0; i < 3*count; i += 3) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
Expand Down Expand Up @@ -2735,7 +2735,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 4 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[4*count-1];
var view = miniTempWebGLFloatBuffers[4*count];
// hoist the heap out of the loop for size and for pthreads+growth.
var heap = HEAPF32;
value = {{{ getHeapOffset('value', 'float') }}};
Expand Down Expand Up @@ -2783,7 +2783,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 4 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[4*count-1];
var view = miniTempWebGLFloatBuffers[4*count];
for (var i = 0; i < 4*count; i += 4) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
Expand Down Expand Up @@ -2827,7 +2827,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 9 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[9*count-1];
var view = miniTempWebGLFloatBuffers[9*count];
for (var i = 0; i < 9*count; i += 9) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
Expand Down Expand Up @@ -2876,7 +2876,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 16 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[16*count-1];
var view = miniTempWebGLFloatBuffers[16*count];
// hoist the heap out of the loop for size and for pthreads+growth.
var heap = HEAPF32;
value = {{{ getHeapOffset('value', 'float') }}};
Expand Down
5 changes: 5 additions & 0 deletions test/browser/webgl_draw_triangle_with_uniform_color.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ int main() {

float color2[4] = { 0.0f, 1.f, 0.0f, 1.0f };
glUniform4fv(glGetUniformLocation(program, "color2"), 1, color2);

// Test that passing zero for the size paramater does not cause error
// https://github.com/emscripten-core/emscripten/issues/21567
glUniform4fv(glGetUniformLocation(program, "color2"), 0, color2);

glClearColor(0.3f,0.3f,0.3f,1);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
Expand Down
9 changes: 9 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4500,6 +4500,15 @@ def test_webgl_offscreen_framebuffer_state_restoration(self):
cmd = args + ['-lGL', '-sOFFSCREEN_FRAMEBUFFER', '-DEXPLICIT_SWAP=1']
self.btest_exit('webgl_offscreen_framebuffer_swap_with_bad_state.c', args=cmd)

@parameterized({
'': ([],),
'es2': (['-sFULL_ES2'],),
'es3': (['-sFULL_ES3'],),
})
@requires_graphics_hardware
def test_webgl_draw_triangle_with_uniform_color(self, args):
self.btest_exit('webgl_draw_triangle_with_uniform_color.c', args=args)

# Tests that using an array of structs in GL uniforms works.
@requires_graphics_hardware
def test_webgl_array_of_structs_uniform(self):
Expand Down

0 comments on commit 5fd7fc0

Please sign in to comment.