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
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 27ff4fd
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 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
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4539,
"a.js.gz": 2315,
"a.js": 4541,
"a.js.gz": 2317,
"a.wasm": 10454,
"a.wasm.gz": 6727,
"total": 15562,
"total_gz": 9421
"total": 15564,
"total_gz": 9423
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 354,
"a.html.gz": 266,
"a.js": 22282,
"a.js.gz": 11608,
"total": 22636,
"total_gz": 11874
"a.js": 22284,
"a.js.gz": 11611,
"total": 22638,
"total_gz": 11877
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4056,
"a.js.gz": 2152,
"a.js": 4061,
"a.js.gz": 2154,
"a.wasm": 10454,
"a.wasm.gz": 6727,
"total": 15079,
"total_gz": 9258
"total": 15084,
"total_gz": 9260
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 354,
"a.html.gz": 266,
"a.js": 21784,
"a.js.gz": 11435,
"total": 22138,
"total_gz": 11701
"a.js": 21789,
"a.js.gz": 11437,
"total": 22143,
"total_gz": 11703
}
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 27ff4fd

Please sign in to comment.