From 61d80ba393cd816802f54a7ac43872a5a49ace90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Thu, 2 Jul 2026 20:12:03 +0300 Subject: [PATCH 1/4] Fix issue with glReadPixels() not taking into account the GL_PACK_ROW_LENGTH, when WEBGL_USE_GARBAGE_FREE_APIS=false, e.g. in Wasm64 mode, due to https://crbug.com/324992397 and https://bugzil.la/1838218. --- src/lib/libwebgl.js | 15 ++++++++- src/struct_info.json | 1 + src/struct_info_generated.json | 1 + .../webgl2_readpixels_pack_row_length.c | 31 +++++++++++++++++++ test/test_browser.py | 4 +++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/browser/webgl2_readpixels_pack_row_length.c diff --git a/src/lib/libwebgl.js b/src/lib/libwebgl.js index bc6a602734487..27e2eb065c899 100644 --- a/src/lib/libwebgl.js +++ b/src/lib/libwebgl.js @@ -330,6 +330,10 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}; unpackAlignment: 4, // default alignment is 4 bytes unpackRowLength: 0, +#if INCLUDE_WEBGL1_FALLBACK && MAX_WEBGL_VERSION >= 2 + packRowLength: 0, +#endif + // Records a GL error condition that occurred, stored until user calls // glGetError() to fetch it. As per GLES2 spec, only the first error is // remembered, and subsequent errors are discarded until the user has @@ -1289,6 +1293,10 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}; GL.unpackAlignment = param; } else if (pname == {{{ cDefs.GL_UNPACK_ROW_LENGTH }}}) { GL.unpackRowLength = param; +#if INCLUDE_WEBGL1_FALLBACK && MAX_WEBGL_VERSION >= 2 + } else if (pname == {{{ cDefs.GL_PACK_ROW_LENGTH }}}) { + GL.packRowLength = param; +#endif } GLctx.pixelStorei(pname, param); }, @@ -1773,7 +1781,12 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}}; } #endif #if INCLUDE_WEBGL1_FALLBACK - var pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height, pixels); +#if MAX_WEBGL_VERSION >= 2 + var stride = Math.max(width, GL.packRowLength); +#else + var stride = width; // GL_PACK_ROW_LENGTH does not exist yet in WebGL 1. +#endif + var pixelData = emscriptenWebGLGetTexPixelData(type, format, stride, height, pixels); if (!pixelData) { GL.recordError(0x500/*GL_INVALID_ENUM*/); #if GL_ASSERTIONS diff --git a/src/struct_info.json b/src/struct_info.json index e4852a0b44b92..88e0afb7fd228 100644 --- a/src/struct_info.json +++ b/src/struct_info.json @@ -1317,6 +1317,7 @@ { "file": "GL/gl.h", "defines": [ + "GL_PACK_ROW_LENGTH", "GL_UNPACK_ALIGNMENT", "GL_UNPACK_ROW_LENGTH" ] diff --git a/src/struct_info_generated.json b/src/struct_info_generated.json index 54c755a8dd05c..9517cd8a3333e 100644 --- a/src/struct_info_generated.json +++ b/src/struct_info_generated.json @@ -301,6 +301,7 @@ "File::DirectoryKind": 2, "File::SymlinkKind": 3, "File::UnknownKind": 0, + "GL_PACK_ROW_LENGTH": 3330, "GL_UNPACK_ALIGNMENT": 3317, "GL_UNPACK_ROW_LENGTH": 3314, "ICANON": 2, diff --git a/test/browser/webgl2_readpixels_pack_row_length.c b/test/browser/webgl2_readpixels_pack_row_length.c new file mode 100644 index 0000000000000..00484e8514810 --- /dev/null +++ b/test/browser/webgl2_readpixels_pack_row_length.c @@ -0,0 +1,31 @@ +// Tests that glReadPixels() works when GL_PACK_ROW_LENGTH > read width. +#include +#include +#include +#include + +int main() { + EmscriptenWebGLContextAttributes attrs; + emscripten_webgl_init_context_attributes(&attrs); + attrs.majorVersion = 2; + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attrs); + emscripten_webgl_make_context_current(ctx); + + // Clear framebuffer to red. + glClearColor(1, 0, 0, 1); + glClear(GL_COLOR_BUFFER_BIT); + + // Read 8 pixels wide into a buffer with row length 32. + glPixelStorei(GL_PACK_ROW_LENGTH, 32); + GLubyte buf[32 * 2 * 4]; + memset(buf, 0, sizeof(buf)); + glReadPixels(0, 0, 8, 2, GL_RGBA, GL_UNSIGNED_BYTE, buf); + + // Verify pixel at (0,0) is red. + assert(buf[0] == 255 && buf[1] == 0 && buf[2] == 0 && buf[3] == 255); + + // Verify that pixel at (0,1) was read to address at stride 32 (pixels), so is red as well. + assert(buf[32*4] == 255 && buf[32*4+1] == 0 && buf[32*4+2] == 0 && buf[32*4+3] == 255); + + return 0; +} diff --git a/test/test_browser.py b/test/test_browser.py index e4dd6ebeabe35..fb08607873a04 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1255,6 +1255,10 @@ def test_webgl2_ubo_layout_binding(self): def test_webgl2_texsubimage3d(self): self.btest_exit('webgl2_texsubimage3d.c', cflags=['-sMIN_WEBGL_VERSION=2']) + @requires_webgl2 + def test_webgl2_readpixels(self): + self.btest_exit('webgl2_readpixels_pack_row_length.c', cflags=['-sMIN_WEBGL_VERSION=2']) + # Test that -sGL_PREINITIALIZED_CONTEXT works and allows user to set Module['preinitializedWebGLContext'] to a preinitialized WebGL context. @requires_graphics_hardware def test_preinitialized_webgl_context(self): From c721b5de949be374097ee1d09385e8d7a6e74895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Thu, 19 Nov 2026 14:09:19 +0200 Subject: [PATCH 2/4] Address review and update to latest main - Zero-initialize the buffer in the test with `= {0}` instead of calling memset(). - Regenerate `struct_info_generated_wasm64.json`, which was missing the new `GL_PACK_ROW_LENGTH` define (`other.test_gen_struct_info`). - Refer to `GL_PACK_ROW_LENGTH` directly, the same way as the other GL constants are now referred to since #28144. Co-Authored-By: Claude Fable 5.2 --- src/lib/libwebgl.js | 2 +- src/struct_info_generated_wasm64.json | 1 + test/browser/webgl2_readpixels_pack_row_length.c | 4 +--- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib/libwebgl.js b/src/lib/libwebgl.js index fc031bd45c140..40095e71890ea 100644 --- a/src/lib/libwebgl.js +++ b/src/lib/libwebgl.js @@ -1267,7 +1267,7 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { } else if (pname == GL_UNPACK_ROW_LENGTH) { GL.unpackRowLength = param; #if INCLUDE_WEBGL1_FALLBACK && MAX_WEBGL_VERSION >= 2 - } else if (pname == {{{ cDefs.GL_PACK_ROW_LENGTH }}}) { + } else if (pname == GL_PACK_ROW_LENGTH) { GL.packRowLength = param; #endif } diff --git a/src/struct_info_generated_wasm64.json b/src/struct_info_generated_wasm64.json index 51b62db2ea12f..87e5b07f41090 100644 --- a/src/struct_info_generated_wasm64.json +++ b/src/struct_info_generated_wasm64.json @@ -890,6 +890,7 @@ "GL_OPERAND1_RGB": 34193, "GL_OPERAND2_ALPHA": 34202, "GL_OPERAND2_RGB": 34194, + "GL_PACK_ROW_LENGTH": 3330, "GL_PIXEL_PACK_BUFFER": 35051, "GL_PIXEL_PACK_BUFFER_BINDING": 35053, "GL_PIXEL_UNPACK_BUFFER": 35052, diff --git a/test/browser/webgl2_readpixels_pack_row_length.c b/test/browser/webgl2_readpixels_pack_row_length.c index 00484e8514810..cc404b48fe930 100644 --- a/test/browser/webgl2_readpixels_pack_row_length.c +++ b/test/browser/webgl2_readpixels_pack_row_length.c @@ -2,7 +2,6 @@ #include #include #include -#include int main() { EmscriptenWebGLContextAttributes attrs; @@ -17,8 +16,7 @@ int main() { // Read 8 pixels wide into a buffer with row length 32. glPixelStorei(GL_PACK_ROW_LENGTH, 32); - GLubyte buf[32 * 2 * 4]; - memset(buf, 0, sizeof(buf)); + GLubyte buf[32 * 2 * 4] = {0}; glReadPixels(0, 0, 8, 2, GL_RGBA, GL_UNSIGNED_BYTE, buf); // Verify pixel at (0,0) is red. From 6b934b5d5ac57f12e240212ecc42b13066270d8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Thu, 19 Nov 2026 14:10:48 +0200 Subject: [PATCH 3/4] Update codesize expectation for test_codesize_hello_dylink_all The glReadPixels() change adds 31 bytes of JS to this test. The Wasm size is not affected. Co-Authored-By: Claude Fable 5.2 --- test/codesize/test_codesize_hello_dylink_all.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index f23945f0e8487..3877004786233 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 268207, + "a.out.js": 268238, "a.out.nodebug.wasm": 535626, - "total": 803833, + "total": 803864, "sent": [ "IMG_Init", "IMG_Load", From 487481d7eb4c50bb5eeb270bd645b58c37ca88f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Thu, 19 Nov 2026 21:25:03 +0200 Subject: [PATCH 4/4] Rename test to match its source file name Co-Authored-By: Claude Fable 5.2 --- test/test_browser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_browser.py b/test/test_browser.py index 1aaeb45b0f91e..6980f156a3fee 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1248,7 +1248,7 @@ def test_webgl2_texsubimage3d(self): self.btest_exit('webgl2_texsubimage3d.c', cflags=['-sMIN_WEBGL_VERSION=2']) @requires_webgl2 - def test_webgl2_readpixels(self): + def test_webgl2_readpixels_pack_row_length(self): self.btest_exit('webgl2_readpixels_pack_row_length.c', cflags=['-sMIN_WEBGL_VERSION=2']) # Test that -sGL_PREINITIALIZED_CONTEXT works and allows user to set Module['preinitializedWebGLContext'] to a preinitialized WebGL context.