diff --git a/src/lib/libwebgl.js b/src/lib/libwebgl.js index 9ba8e60056fa6..40095e71890ea 100644 --- a/src/lib/libwebgl.js +++ b/src/lib/libwebgl.js @@ -312,6 +312,10 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { 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 @@ -1262,6 +1266,10 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { GL.unpackAlignment = param; } else if (pname == GL_UNPACK_ROW_LENGTH) { GL.unpackRowLength = param; +#if INCLUDE_WEBGL1_FALLBACK && MAX_WEBGL_VERSION >= 2 + } else if (pname == GL_PACK_ROW_LENGTH) { + GL.packRowLength = param; +#endif } GLctx.pixelStorei(pname, param); }, @@ -1718,7 +1726,12 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { } #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(GL_INVALID_ENUM); #if GL_ASSERTIONS diff --git a/src/struct_info.json b/src/struct_info.json index 8f11e2c7dd6f1..91a4ca73ef8e4 100644 --- a/src/struct_info.json +++ b/src/struct_info.json @@ -1973,6 +1973,7 @@ "GL_OPERAND1_RGB", "GL_OPERAND2_ALPHA", "GL_OPERAND2_RGB", + "GL_PACK_ROW_LENGTH", "GL_PIXEL_PACK_BUFFER", "GL_PIXEL_PACK_BUFFER_BINDING", "GL_PIXEL_UNPACK_BUFFER", diff --git a/src/struct_info_generated.json b/src/struct_info_generated.json index fd53302638b0d..48f570022a6a3 100644 --- a/src/struct_info_generated.json +++ b/src/struct_info_generated.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/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 new file mode 100644 index 0000000000000..cc404b48fe930 --- /dev/null +++ b/test/browser/webgl2_readpixels_pack_row_length.c @@ -0,0 +1,29 @@ +// Tests that glReadPixels() works when GL_PACK_ROW_LENGTH > read width. +#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] = {0}; + 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/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", diff --git a/test/test_browser.py b/test/test_browser.py index 800548a527f73..6980f156a3fee 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1247,6 +1247,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_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. @requires_graphics_hardware def test_preinitialized_webgl_context(self):