You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi!
I got the error "Uncaught TypeError TypeError: Failed to execute 'compressedTexImage2D' on 'WebGLRenderingContext': parameter 7 is not of type 'ArrayBufferView'", when I called glCompressedTexImage2D and passed null to the "data" parameter. How to avoid it? Is there any workaround?
Its hard to say what this might be without more details. Can you share a full backtrace? And the line of code at the call site? If possible a complete program would be even better.
There are several tests in the test directory can use glCompressedTexImage2D. It might be useful to take a look at test/browser/test_anisotropic.c, for example, to see if there is anything that you are doing differently.
We started experiencing this problem too after upgrading from emscripten version 3.1.39 to version 3.1.64. Here is a minimal repro based on the anisotropic test:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_opengl.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
// Slightly different SDL initialization
if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_Surface *screen = SDL_SetVideoMode( 600, 600, 16, SDL_OPENGL );
if ( !screen ) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
// Clear the screen before drawing
glClear( GL_COLOR_BUFFER_BIT );
glViewport( 0, 0, 600, 600 );
GLuint texture;
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0, 8, NULL);
SDL_GL_SwapBuffers();
#ifndef __EMSCRIPTEN__
// Wait for 3 seconds to give us a chance to see the image
SDL_Delay(2000);
#endif
// Now we can delete the OpenGL texture and close down SDL
glDeleteTextures( 1, &texture );
SDL_Quit();
return 0;
}
main.js:3810 Uncaught TypeError: Failed to execute 'compressedTexImage2D' on 'WebGL2RenderingContext': parameter 7 is not of type 'ArrayBufferView'.
at _glCompressedTexImage2D (main.js:3810:9)
at __main_argc_argv (test_anisotropic.c:31)
at main.js:634:12
at callMain (main.js:3973:15)
at doRun (main.js:4011:23)
at main.js:4020:7
The two necessary conditions to reproduce the issue are to use glCompressedTexImage2D with NULL pointer for the last argument and to compile with memory limit greater than 2GB.
This is how glCompressedTexImage2D implementation looks in 3.1.39:
glCompressedTexImage2D: function(target, level, internalFormat, width, height, border, imageSize, data) {
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
if (GLctx.currentPixelUnpackBufferBinding || !imageSize) {
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, imageSize, data);
} else {
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize);
}
return;
}
#endif
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, data ? {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}} : null);
},
The different part is WEBGL_USE_GARBAGE_FREE_APIS which is false with higher memory limit. I don't know enough about emscripten to fix this properly, but hopefully now the issue will get addressed.
What is the glCompressedTexImage2D supposed to be doing here: glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0, 8, NULL);?
i.e. what does it mean to set the pixels argument to NULL?
It seems like before #21445 we would end up calling GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize); which I think would read imageSize bytes from address zero (data is zero/null) of the main memoy.. which seem wrong.
I'm not sure how this is supposed to work I guess. @juj might know? Also @kainino0x ?
According the MDN docs it should be possible to call compressedTexImage2D with only 6 arguments. But according the errors I'm seeing in chrome it is requiring the a 7th argument.
Actually the 6 argument version fails here with webgl1 too: TypeError: Failed to execute 'compressedTexImage2D' on 'WebGLRenderingContext': 7 arguments required, but only 6 present.
So this code also fails without memory growth on WebGL1: TypeError: Failed to execute 'compressedTexImage2D' on 'WebGLRenderingContext': parameter 7 is not of type 'ArrayBufferView'.
Doing a little research it sounds like passing null for pixels is generally don't to reserve space for following calls to glCompressedTexSubImage2D? Is that why you are passing null in this case?
From the docs it looks like maybe glTexImage2D supports this null but glCompressedTexSubImage2D does not?
Node that glTexImage2D mentions "data may be a null pointer. In this case, texture memory is allocated to accommodate a texture of width width and height height." but glCompressedTexImage2D docs do no say this.
Yes, we pass NULL to allocate space for the texture. I tried that suggestion with glTexImage2D, but it does not work. In desktop GL (NVidia) the output is all black and in Chrome I get some error (I don't remember what it was exactly, but if you want you can try to modify the repro sample). I also noticed that reference docs do not explicitly mention NULL, but docs are not specification and it seems only logical to me that glCompressedTexImage2D/glCompressedTexSubImage2D would behave analogously to glTexImage2D/glTexSubImage2D.
If the data argument of CompressedTexImage1D, CompressedTexImage2D, or CompressedTexImage3D is NULL, and the pixel unpack buffer object is zero, a texture image with unspecified image contents is created, just as when a NULL pointer is passed to TexImage1D, TexImage2D, or TexImage3D.
Perhaps we can map compressedTexImage2D to texImage2D internally in the case the pixels is NULL. According the webgl spec pixels cannot be null for compressedTexImage2D, only for texImage2D, although that might be an oversight in the spec?
texImage2D in the same doc says "If pixels is null, a buffer of sufficient size initialized to 0 is passed." and indeed has a signature where the final argument is optional unlike compressedTexImage2D.
And how about the first overload compressedTexImage2D(target, level, internalformat, width, height, border) ? I don't see any way to pass a pointer to data to it, so I assume it must be intended for the texture allocation use case. Otherwise it would be useless. Alternatively the GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize); version seems to work well as a workaround.
Well, so I did some tests. That overload did not work (maybe because I have WebGL 2 context and not 1), but GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize); works fine. glTexImage2D generates WebGL: INVALID_OPERATION: texImage2D: compressed texture formats are not accepted
@matusfedorko does you sample code above actually draw anything to screen? I'm like to use it as a test case for landing this fix, but it would be good if it actually used a compressed subimage and drew something I think.
No, but feel free to adjust it so that it does. I did not have time to make anything fancy, but a barebones sample that demonstrates the issue clearly.
What about the idea of using textImage2D instead of compressedTexImage2D when data in null
Unfortunately calling texImage2D will establish the GPU side backing store of the texture to contain an uncompressed texture, and not a compressed texture. Later calling compressedTexSubImage2D to attempt to upload compressed texture data to it would cause an error since the types wouldn't match.
Or later calling compressedTexImage2D on the texture would just redo/discard the GPU side memory allocation that the earlier texImage2D call had made.
I see. How about passing new ArrayBuffer(size) ? Would that work? I guess it would create garbage.. but maybe this API is not called too often? Maybe OK, at least until the upstream issue is fixed?
Or we could us a static empty buffer than we realloc each time? That would have a constant size overhead.
From the beginning of the WebGL spec, compressedTexImage2D was expected to receive valid compressed texture data.
It was only during the introduction of WebGL 2.0, when applications could control textures' internalformat, that the possibility of allocating an "empty" compressed texture was introduced into the API. At that point it became valid to allocate a compressed texture using texStorage2D (or 3D) and fill its levels with compressedTexSubImage2D (or 3D).
Therefore, yes, if the application passes NULL to glCompressedTexImage2D and is using WebGL 2.0 under the hood, it would be valid to call texStorage2D with the correct format and internalformat to allocate it. If it's using WebGL 1.0 then I would recommend synthesizing an OpenGL error. An alternative is to allocate a large zeroed buffer (the necessary size of this buffer is defined in the various WebGL compressed texture specs like that for S3TC), but I would strongly recommend against this. Such allocations are not cheap and are also not guaranteed to be promptly garbage collected.
@matusfedorko there is no overload compressedTexImage2D(target, level, internalformat, width, height, border). The overloads can be found here:
This is the part that doesn't seem to be working according to @matusfedorko
Note that texImage2D and texStorage2D are different functionalities of OpenGL/WebGL.
We can not call texStorage2D as a special-cased path inside compressedTexImage2D. This is because there are many functional differences (see OpenGL ES 3.0 specification 3.8.4 Immutable-Format Texture Images) that would result.
How about passing new ArrayBuffer(size) ? Would that work?
That would work, but would be pretty disappointing. If someone has their "GPU memory is compromised, but CPU memory is intact" op-sec hat on, then we could certainly do that.
How about passing new ArrayBuffer(size) ? Would that work?
That would work, but would be pretty disappointing.
Agreed; as I mentioned above, such allocations are not cheap and are also not guaranteed to be promptly garbage collected. It would be better to initialize the compressed texture with whatever random data is at address 0 in the Wasm heap, as Jukka points out the code used to work before.
How about passing new ArrayBuffer(size) ? Would that work?
That would work, but would be pretty disappointing.
Agreed; as I mentioned above, such allocations are not cheap and are also not guaranteed to be promptly garbage collected. It would be better to initialize the compressed texture with whatever random data is at address 0 in the Wasm heap, as Jukka points out the code used to work before.
How about reusing a global allocation:
if (!pixels) {
pixels = globalTmpBuf = realloc(globalTmpBuf, size);
}
That would limit us to single allocation that only happens rarely, and avoid reading from address zero.
Regardless is seems like a bug in the spec that the final argument it not optional right?
Textures in 3D applications tend to be very large. This global allocation will be wasted space that is only present in order to give these compressed textures zeroed-out data. The data in the Wasm heap is controlled entirely by the application; there is no chance that any data uploaded to the texture will cause a leak of information that the application shouldn't be able to see.
The guarantees in OpenGL ES's C API are much weaker than those provided by WebGL, where nearly all undefined behavior was resolved and turned into well-defined behavior. Applications compiled with Emscripten are only relying on the C API's semantics.
For this reason I once again strongly support @juj 's suggestion to just upload data from the 0 address in the Wasm heap if the app calls glCompressedTexImage2D with a NULL pointer. This behavior is 100% compatible with the C API's semantics.
Textures in 3D applications tend to be very large. This global allocation will be wasted space that is only present in order to give these compressed textures zeroed-out data. The data in the Wasm heap is controlled entirely by the application; there is no chance that any data uploaded to the texture will cause a leak of information that the application shouldn't be able to see.
The guarantees in OpenGL ES's C API are much weaker than those provided by WebGL, where nearly all undefined behavior was resolved and turned into well-defined behavior. Applications compiled with Emscripten are only relying on the C API's semantics.
For this reason I once again strongly support @juj 's suggestion to just upload data from the 0 address in the Wasm heap if the app calls glCompressedTexImage2D with a NULL pointer. This behavior is 100% compatible with the C API's semantics.
OK lets go with that previous/odd behaviour.
Is it compatible though? I thought passing NULL as the last argument guaranteed that the new space was zero-initialized. The data at address zero in the wasm memory is likely not going to be zeros. We put C static data at address 1024, and we write cookies to the first 8 bytes of memory.
Is it compatible though? I thought passing NULL as the last argument guaranteed that the new space was zero-initialized. The data at address zero in the wasm memory is likely not going to be zeros. We put C static data at address 1024, and we write cookies to the first 8 bytes of memory.
The GLES 3.0 spec says (per Jukka's comment above) that if NULL is passed as the data argument, the image's contents are unspecified. This behavior is compatible.
Activity
konstantinegorov commentedon Aug 17, 2023
Is there any update?
sbc100 commentedon Aug 17, 2023
Its hard to say what this might be without more details. Can you share a full backtrace? And the line of code at the call site? If possible a complete program would be even better.
There are several tests in the
testdirectory can useglCompressedTexImage2D. It might be useful to take a look attest/browser/test_anisotropic.c, for example, to see if there is anything that you are doing differently.matusfedorko commentedon Aug 15, 2024
We started experiencing this problem too after upgrading from emscripten version 3.1.39 to version 3.1.64. Here is a minimal repro based on the anisotropic test:
Compile like this:
The output is:
The two necessary conditions to reproduce the issue are to use
glCompressedTexImage2Dwith NULL pointer for the last argument and to compile with memory limit greater than 2GB.This is how glCompressedTexImage2D implementation looks in 3.1.39:
And this is how it looks in 3.1.64:
The different part is
WEBGL_USE_GARBAGE_FREE_APISwhich is false with higher memory limit. I don't know enough about emscripten to fix this properly, but hopefully now the issue will get addressed.kripken commentedon Aug 16, 2024
Thanks for the testcase @matusfedorko , I bisected this to "Avoid garbage-free WebGL APIs when memory size is over 2gb." (#21445)
@sbc100 PTAL
sbc100 commentedon Aug 19, 2024
What is the
glCompressedTexImage2Dsupposed to be doing here:glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, 4, 4, 0, 8, NULL);?i.e. what does it mean to set the pixels argument to NULL?
It seems like before #21445 we would end up calling
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize);which I think would readimageSizebytes from address zero (data is zero/null) of the main memoy.. which seem wrong.I'm not sure how this is supposed to work I guess. @juj might know? Also @kainino0x ?
sbc100 commentedon Aug 19, 2024
According the MDN docs it should be possible to call
compressedTexImage2Dwith only 6 arguments. But according the errors I'm seeing in chrome it is requiring the a 7th argument.sbc100 commentedon Aug 19, 2024
Actually the 6 argument version fails here with webgl1 too:
TypeError: Failed to execute 'compressedTexImage2D' on 'WebGLRenderingContext': 7 arguments required, but only 6 present.sbc100 commentedon Aug 19, 2024
So this code also fails without memory growth on WebGL1:
TypeError: Failed to execute 'compressedTexImage2D' on 'WebGLRenderingContext': parameter 7 is not of type 'ArrayBufferView'.sbc100 commentedon Aug 22, 2024
Doing a little research it sounds like passing
nullfor pixels is generally don't to reserve space for following calls to glCompressedTexSubImage2D? Is that why you are passingnullin this case?From the docs it looks like maybe glTexImage2D supports this
nullbut glCompressedTexSubImage2D does not?See: https://community.khronos.org/t/glcompressedteximage2d-and-null-data/41505
And: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
And: https://registry.khronos.org/OpenGL-Refpages/gl4/html/glCompressedTexImage2D.xhtml
Node that glTexImage2D mentions "data may be a null pointer. In this case, texture memory is allocated to accommodate a texture of width width and height height." but
glCompressedTexImage2Ddocs do no say this.ping @kainino0x @juj
matusfedorko commentedon Aug 22, 2024
Yes, we pass NULL to allocate space for the texture. I tried that suggestion with glTexImage2D, but it does not work. In desktop GL (NVidia) the output is all black and in Chrome I get some error (I don't remember what it was exactly, but if you want you can try to modify the repro sample). I also noticed that reference docs do not explicitly mention NULL, but docs are not specification and it seems only logical to me that glCompressedTexImage2D/glCompressedTexSubImage2D would behave analogously to glTexImage2D/glTexSubImage2D.
matusfedorko commentedon Aug 22, 2024
Actually. in the OpenGL 4.6 specification (https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf) on page 230 I found this:
If the data argument of CompressedTexImage1D, CompressedTexImage2D, or CompressedTexImage3D is NULL, and the pixel unpack buffer object is zero, a texture image with unspecified image contents is created, just as when a NULL pointer is passed to TexImage1D, TexImage2D, or TexImage3D.sbc100 commentedon Aug 22, 2024
Sadly i looks like I can't omit the last argument or set it to null using WebGL2's
compressedTexImage2D. See #19300 (comment).I wonder if this is bug since https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/compressedTexImage2D mentions a six argument version in WebGL1 and says that that WebGL API adds overloads.
sbc100 commentedon Aug 22, 2024
Perhaps we can map
compressedTexImage2DtotexImage2Dinternally in the case the pixels is NULL. According the webgl specpixelscannot be null forcompressedTexImage2D, only fortexImage2D, although that might be an oversight in the spec?https://registry.khronos.org/webgl/specs/latest/1.0/#COMPRESSEDTEXIMAGE2D
texImage2Din the same doc says "If pixels is null, a buffer of sufficient size initialized to 0 is passed." and indeed has a signature where the final argument is optional unlikecompressedTexImage2D.@kenrussell maybe you can help here?
matusfedorko commentedon Aug 23, 2024
And how about the first overload
compressedTexImage2D(target, level, internalformat, width, height, border)? I don't see any way to pass a pointer to data to it, so I assume it must be intended for the texture allocation use case. Otherwise it would be useless. Alternatively theGLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize);version seems to work well as a workaround.matusfedorko commentedon Aug 23, 2024
Well, so I did some tests. That overload did not work (maybe because I have WebGL 2 context and not 1), but
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize);works fine.glTexImage2DgeneratesWebGL: INVALID_OPERATION: texImage2D: compressed texture formats are not accepted5 remaining items
matusfedorko commentedon Aug 23, 2024
No, but feel free to adjust it so that it does. I did not have time to make anything fancy, but a barebones sample that demonstrates the issue clearly.
sbc100 commentedon Aug 23, 2024
I see. How about passing
new ArrayBuffer(size)? Would that work? I guess it would create garbage.. but maybe this API is not called too often? Maybe OK, at least until the upstream issue is fixed?Or we could us a static empty buffer than we
realloceach time? That would have a constant size overhead.kenrussell commentedon Aug 23, 2024
From the beginning of the WebGL spec,
compressedTexImage2Dwas expected to receive valid compressed texture data.It was only during the introduction of WebGL 2.0, when applications could control textures'
internalformat, that the possibility of allocating an "empty" compressed texture was introduced into the API. At that point it became valid to allocate a compressed texture usingtexStorage2D(or 3D) and fill its levels withcompressedTexSubImage2D(or 3D).Therefore, yes, if the application passes NULL to
glCompressedTexImage2Dand is using WebGL 2.0 under the hood, it would be valid to calltexStorage2Dwith the correctformatandinternalformatto allocate it. If it's using WebGL 1.0 then I would recommend synthesizing an OpenGL error. An alternative is to allocate a large zeroed buffer (the necessary size of this buffer is defined in the various WebGL compressed texture specs like that for S3TC), but I would strongly recommend against this. Such allocations are not cheap and are also not guaranteed to be promptly garbage collected.@matusfedorko there is no overload
compressedTexImage2D(target, level, internalformat, width, height, border). The overloads can be found here:https://registry.khronos.org/webgl/specs/latest/1.0/
https://registry.khronos.org/webgl/specs/latest/2.0/
and are:
For WebGL 1.0:
undefined compressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, [AllowShared] ArrayBufferView data);From WebGL 2.0, taking the compressed texture data from a (GPU-side) pixel buffer object:
undefined compressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, GLintptr offset);and WebGL 2.0's "garbage-free" entry point which selects the region from the passed ArrayBufferView:
undefined compressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, [AllowShared] ArrayBufferView srcData, optional unsigned long long srcOffset = 0, optional GLuint srcLengthOverride = 0);sbc100 commentedon Aug 23, 2024
This is the part that doesn't seem to be working according to @matusfedorko. Hopefully I can create a test/sample to verify this.
matusfedorko commentedon Aug 23, 2024
Oh wait, but
texStorage2Dis different thantexImage2D. I tried the latter one. I thinktexStorage2Dcould work.Thanks for the info. I went off of the MDN docs in my comment, which show that overload in the first place.
juj commentedon Aug 23, 2024
Note that
texImage2DandtexStorage2Dare different functionalities of OpenGL/WebGL.We can not call
texStorage2Das a special-cased path insidecompressedTexImage2D. This is because there are many functional differences (see OpenGL ES 3.0 specification 3.8.4 Immutable-Format Texture Images) that would result.That would work, but would be pretty disappointing. If someone has their "GPU memory is compromised, but CPU memory is intact" op-sec hat on, then we could certainly do that.
kenrussell commentedon Aug 23, 2024
Agreed; as I mentioned above, such allocations are not cheap and are also not guaranteed to be promptly garbage collected. It would be better to initialize the compressed texture with whatever random data is at address 0 in the Wasm heap, as Jukka points out the code used to work before.
sbc100 commentedon Aug 23, 2024
How about reusing a global allocation:
That would limit us to single allocation that only happens rarely, and avoid reading from address zero.
Regardless is seems like a bug in the spec that the final argument it not optional right?
kenrussell commentedon Aug 23, 2024
Textures in 3D applications tend to be very large. This global allocation will be wasted space that is only present in order to give these compressed textures zeroed-out data. The data in the Wasm heap is controlled entirely by the application; there is no chance that any data uploaded to the texture will cause a leak of information that the application shouldn't be able to see.
The guarantees in OpenGL ES's C API are much weaker than those provided by WebGL, where nearly all undefined behavior was resolved and turned into well-defined behavior. Applications compiled with Emscripten are only relying on the C API's semantics.
For this reason I once again strongly support @juj 's suggestion to just upload data from the 0 address in the Wasm heap if the app calls
glCompressedTexImage2Dwith a NULL pointer. This behavior is 100% compatible with the C API's semantics.sbc100 commentedon Aug 24, 2024
OK lets go with that previous/odd behaviour.
Is it compatible though? I thought passing NULL as the last argument guaranteed that the new space was zero-initialized. The data at address zero in the wasm memory is likely not going to be zeros. We put C static data at address 1024, and we write cookies to the first 8 bytes of memory.
kenrussell commentedon Aug 24, 2024
The GLES 3.0 spec says (per Jukka's comment above) that if NULL is passed as the
dataargument, the image's contents are unspecified. This behavior is compatible.Fix calls to glCcompressedTexImage2D with null pixel data
Fix calls to glCcompressedTexImage2D with null pixel data
Fix calls to glCcompressedTexImage2D with null pixel data (#22453)