emscripten-core / emscripten-core/emscripten

glPixelStorei does not support PACK_ALIGNMENT

Open
#23,151 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

This issue originates from https://github.com/emscripten-core/emscripten/issues/21968

Calling `glPixelStorei` affects the memory layout of pixel data in `glTexImage2D`, `glTexSubImage2D`, and `glReadPixels`, thereby affecting the valid length of the pixel data. The OpenGL documentation provides a more detailed explanation:
https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glPixelStorei.xhtml

Emscripten restricts the length of pixel data through `emscriptenWebGLGetTexPixelData`, but this restriction may be shorter than expected after calling `glPixelStorei`. This results in browser errors.
Chrome and Safari:
```
WebGL: INVALID_OPERATION: texSubImage2D: ArrayBufferView not big enough for request
```
FireFox:
```
WebGL warning: texSubImage: Desired upload requires more bytes (20004) than are available (1440).
```

The problem with `emscriptenWebGLGetTexPixelData` is that it does not distinguish between pack and unpack, nor does it handle all possible parameters in `glPixelStorei`.

My simple patch is:
```
sed -i'.original' -e 's/return heap.subarray(toTypedArrayIndex(pixels, heap), toTypedArrayIndex(pixels + bytes, heap));/return heap.subarray(toTypedArrayIndex(pixels, heap));/g' emsdk/src/library_webgl.js
```
This removes the length restriction on the pixel data, since the C API only passes a pointer without data length.

The issue with this patch is that it makes it easier for the pixel data length to exceed 2GB, which in FireFox leads to a new problem:
```
WebGL2RenderingContext.readPixels: Argument 7 can't be an ArrayBuffer or an ArrayBufferView larger than 2 GB
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.