Skip to content

Commit 6491f02

Browse files
authored
[webgl] Implement GL_UNPACK_ROW_LENGTH (#21980)
Fixes: #21968
1 parent 579da6c commit 6491f02

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

src/generated_struct_info32.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@
308308
"File::DirectoryKind": 2,
309309
"File::SymlinkKind": 3,
310310
"File::UnknownKind": 0,
311+
"GL_UNPACK_ALIGNMENT": 3317,
312+
"GL_UNPACK_ROW_LENGTH": 3314,
311313
"ICANON": 2,
312314
"ICRNL": 256,
313315
"IEXTEN": 32768,

src/generated_struct_info64.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@
308308
"File::DirectoryKind": 2,
309309
"File::SymlinkKind": 3,
310310
"File::UnknownKind": 0,
311+
"GL_UNPACK_ALIGNMENT": 3317,
312+
"GL_UNPACK_ROW_LENGTH": 3314,
311313
"ICANON": 2,
312314
"ICRNL": 256,
313315
"IEXTEN": 32768,

src/library_webgl.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
288288
#endif
289289

290290
unpackAlignment: 4, // default alignment is 4 bytes
291+
unpackRowLength: 0,
291292

292293
// Records a GL error condition that occurred, stored until user calls
293294
// glGetError() to fetch it. As per GLES2 spec, only the first error is
@@ -1235,8 +1236,10 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
12351236
},
12361237

12371238
glPixelStorei: (pname, param) => {
1238-
if (pname == 0xCF5 /* GL_UNPACK_ALIGNMENT */) {
1239+
if (pname == {{{ cDefs.GL_UNPACK_ALIGNMENT }}}) {
12391240
GL.unpackAlignment = param;
1241+
} else if (pname == {{{ cDefs.GL_UNPACK_ROW_LENGTH }}}) {
1242+
GL.unpackRowLength = param;
12401243
}
12411244
GLctx.pixelStorei(pname, param);
12421245
},
@@ -1548,15 +1551,15 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
15481551
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, data ? {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}} : null);
15491552
},
15501553

1551-
$computeUnpackAlignedImageSize: (width, height, sizePerPixel, alignment) => {
1554+
$computeUnpackAlignedImageSize: (width, height, sizePerPixel) => {
15521555
function roundedToNextMultipleOf(x, y) {
15531556
#if GL_ASSERTIONS
15541557
assert((y & (y-1)) === 0, 'Unpack alignment must be a power of 2! (Allowed values per WebGL spec are 1, 2, 4 or 8)');
15551558
#endif
15561559
return (x + y - 1) & -y;
15571560
}
1558-
var plainRowSize = width * sizePerPixel;
1559-
var alignedRowSize = roundedToNextMultipleOf(plainRowSize, alignment);
1561+
var plainRowSize = (GL.unpackRowLength || width) * sizePerPixel;
1562+
var alignedRowSize = roundedToNextMultipleOf(plainRowSize, GL.unpackAlignment);
15601563
return height * alignedRowSize;
15611564
},
15621565

@@ -1600,7 +1603,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
16001603
$emscriptenWebGLGetTexPixelData: (type, format, width, height, pixels, internalFormat) => {
16011604
var heap = heapObjectForWebGLType(type);
16021605
var sizePerPixel = colorChannelsInGlTextureFormat(format) * heap.BYTES_PER_ELEMENT;
1603-
var bytes = computeUnpackAlignedImageSize(width, height, sizePerPixel, GL.unpackAlignment);
1606+
var bytes = computeUnpackAlignedImageSize(width, height, sizePerPixel);
16041607
#if GL_ASSERTIONS
16051608
assert(pixels % heap.BYTES_PER_ELEMENT == 0, 'Pointer to texture data passed to texture get function must be aligned to the byte size of the pixel type!');
16061609
#endif

src/struct_info.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,13 @@
12041204
"ALC_INVALID_ENUM"
12051205
]
12061206
},
1207+
{
1208+
"file": "GL/gl.h",
1209+
"defines": [
1210+
"GL_UNPACK_ALIGNMENT",
1211+
"GL_UNPACK_ROW_LENGTH"
1212+
]
1213+
},
12071214
// ===========================================
12081215
// WebGPU
12091216
// NOTE: This section is auto-generated.

test/test_browser.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,11 @@ def test_cubegeom_pre2_vao2(self):
21932193
def test_cubegeom_pre_vao_es(self):
21942194
self.reftest('third_party/cubegeom/cubegeom_pre_vao_es.c', 'third_party/cubegeom/cubegeom_pre_vao.png', args=['-sFULL_ES2', '-lGL', '-lSDL'])
21952195

2196+
@requires_graphics_hardware
2197+
@no_swiftshader
2198+
def test_cubegeom_row_length(self):
2199+
self.reftest('third_party/cubegeom/cubegeom_pre_vao_es.c', 'third_party/cubegeom/cubegeom_pre_vao.png', args=['-sFULL_ES2', '-lGL', '-lSDL', '-DUSE_UNPACK_ROW_LENGTH'])
2200+
21962201
@requires_graphics_hardware
21972202
def test_cubegeom_u4fv_2(self):
21982203
self.reftest('third_party/cubegeom/cubegeom_u4fv_2.c', 'third_party/cubegeom/cubegeom_u4fv_2.png', args=['-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])

test/third_party/cubegeom/cubegeom_pre_vao_es.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,25 @@ int main(int argc, char *argv[])
5555
glBindTexture( GL_TEXTURE_2D, texture );
5656
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
5757
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
58-
GLubyte textureData[16*16*4];
58+
#if USE_UNPACK_ROW_LENGTH
59+
#define ROW_SIZE 20
60+
#else
61+
#define ROW_SIZE 16
62+
#endif
63+
GLubyte textureData[16*ROW_SIZE*4] = {0};
5964
for (int x = 0; x < 16; x++) {
6065
for (int y = 0; y < 16; y++) {
61-
*((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8);
66+
*((int*)&textureData[(x*ROW_SIZE + y) * 4]) = x*16 + ((y*16) << 8);
6267
}
6368
}
69+
#if USE_UNPACK_ROW_LENGTH
70+
glPixelStorei(GL_UNPACK_ROW_LENGTH, ROW_SIZE);
71+
#endif
6472
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
6573
GL_RGBA, GL_UNSIGNED_BYTE, textureData );
74+
#if USE_UNPACK_ROW_LENGTH
75+
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
76+
#endif
6677

6778
// Create a second texture
6879

0 commit comments

Comments
 (0)