Skip to content

Commit 2b6781e

Browse files
committed
Fix calls to glCcompressedTexImage2D with null pixel data
I believe this has always been an issue with emscripten's WebGL1 code which only recently became more prevalent with emscripten-core#21445. Fixes: emscripten-core#19300
1 parent 62d7153 commit 2b6781e

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/library_webgl.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,11 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
15201520
},
15211521

15221522
glCompressedTexImage2D: (target, level, internalFormat, width, height, border, imageSize, data) => {
1523+
// `data` may be null here, which means "allocate uniniitalized space but
1524+
// don't upload" in GLES parlance, but `compressedTexImage2D` requires the
1525+
// final data parameter, so we simply pass a heap view starting at zero
1526+
// effectively uploading whatever happens to be near address zero. See
1527+
// https://github.com/emscripten-core/emscripten/issues/19300.
15231528
#if MAX_WEBGL_VERSION >= 2
15241529
if ({{{ isCurrentContextWebGL2() }}}) {
15251530
if (GLctx.currentPixelUnpackBufferBinding || !imageSize) {
@@ -1533,7 +1538,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
15331538
}
15341539
#endif
15351540
#if INCLUDE_WEBGL1_FALLBACK
1536-
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, data ? {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}} : null);
1541+
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}});
15371542
#endif
15381543
},
15391544

@@ -1552,7 +1557,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
15521557
}
15531558
#endif
15541559
#if INCLUDE_WEBGL1_FALLBACK
1555-
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, data ? {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}} : null);
1560+
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}});
15561561
#endif
15571562
},
15581563

test/browser/test_anisotropic.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,12 @@ int main(int argc, char *argv[])
115115
while (level < 5) {
116116
printf("uploading level %d: %d, %d\n", level, w, h);
117117
assert(!glGetError());
118+
#if TEST_TEXSUBIMAGE
119+
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, NULL);
120+
glCompressedTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, w, h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w*h, curr);
121+
#else
118122
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
123+
#endif
119124
assert(!glGetError());
120125
curr += MAX(w, 4)*MAX(h, 4);
121126
w /= 2;
@@ -136,7 +141,12 @@ int main(int argc, char *argv[])
136141
while (level < 5) {
137142
printf("uploading level %d: %d, %d\n", level, w, h);
138143
assert(!glGetError());
144+
#if TEST_TEXSUBIMAGE
145+
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, NULL);
146+
glCompressedTexSubImage2D(GL_TEXTURE_2D, level, 0, 0, w, h, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w*h, curr);
147+
#else
139148
glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr);
149+
#endif
140150
assert(!glGetError());
141151
curr += MAX(w, 4)*MAX(h, 4);
142152
w /= 2;

test/test_browser.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,9 +2185,13 @@ def test_s3tc_ffp_only(self):
21852185
self.reftest('s3tc.c', 's3tc.png', args=['--preload-file', 'screenshot.dds', '-sLEGACY_GL_EMULATION', '-sGL_FFP_ONLY', '-lGL', '-lSDL'])
21862186

21872187
@requires_graphics_hardware
2188-
def test_anisotropic(self):
2188+
@parameterized({
2189+
'': ([],),
2190+
'subimage': (['-DTEST_TEXSUBIMAGE'],),
2191+
})
2192+
def test_anisotropic(self, args):
21892193
shutil.copyfile(test_file('browser/water.dds'), 'water.dds')
2190-
self.reftest('test_anisotropic.c', 'test_anisotropic.png', reference_slack=2, args=['--preload-file', 'water.dds', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-Wno-incompatible-pointer-types'])
2194+
self.reftest('test_anisotropic.c', 'test_anisotropic.png', reference_slack=2, args=['--preload-file', 'water.dds', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL', '-Wno-incompatible-pointer-types'] + args)
21912195

21922196
@requires_graphics_hardware
21932197
def test_tex_nonbyte(self):

0 commit comments

Comments
 (0)