glGenerateMipmap - non-power-of-2
Asked Answered
L

2

6

Environment Ubuntu 17.04, Chrome 60.

Runs this example local without warnings/errors : https://github.com/mdn/webgl-examples/tree/gh-pages/tutorial/sample6

Then replace cubetexture.png with an non-power-of-2 image here : https://github.com/mdn/webgl-examples/blob/gh-pages/tutorial/sample6/webgl-demo.js#L220

Got as expected warnings/errors :

[.Offscreen-For-WebGL-0x13d050ba2c00]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering.

[.Offscreen-For-WebGL-0x13d050ba2c00]GL ERROR :GL_INVALID_OPERATION : glGenerateMipmap: Can not generate mips

Next add parameters :

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);

Then runs ok without warnings, but still unexpected got the error :

[.Offscreen-For-WebGL-0x13d050fb4000]GL ERROR :GL_INVALID_OPERATION : glGenerateMipmap: Can not generate mips

Apart from the error message the non-power-of-2 image is render ok, at the cube. Same behavior in Firefox 54. Any tips of what is going on here?

Lahey answered 1/8, 2017 at 6:39 Comment(0)
P
6

You can not generate a mipmap for a non-power-of-2 texture in WebGL1. That's kind of the point, mipmapped non-power-of-2 textures are not supported in WebGL1 period. So you set the filtering so it uses only level 0

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);

Which means there's no reason to generate mips since they'll never be used. See this article.

Penetralia answered 1/8, 2017 at 12:42 Comment(1)
According to OpenGL ES 2.0 Specificiation - Section 3.7.11 / Page 84Arenas
K
2

WebGL is based on OpenGL ES 2.0. While online documentation for WebGL defines available functions, but for restrictions and some of specific behavior you would need to address OpenGL ES 2.0 full spec. It's available at https://www.khronos.org/registry/OpenGL/specs/es/2.0/es_full_spec_2.0.pdf

For TexImage2D function in 3.7.1:

If level is greater than zero, and either width or height is not a power of two, the error INVALID_VALUE is generated

And for GenerateMipmap in 3.7.11:

If either the width or height of the level zero array are not a power or two, the error INVALID_OPERATION is generated

Conclusion

You can have only first(zero) level for non-power-of-two texture. And you cant generate mipmaps for non-power-of-two texture.


Also it's worth noting that non-power-of-two texture has additional limitations:

In article 3.7:

If any dimension of any array in a mipmap is not a power of two (e.g. if rounding down as described above is performed), then the mipmap is described as a non-power-of-two texture. Non-power-of-two textures have restrictions on the allowed texture wrap modes and filters, as described in section 3.8.2

In 3.8.2:

Calling a sampler from a fragment shader will return (R, G, B, A) = (0, 0, 0, 1) if any of the following conditions are true:

....

• A two-dimensional sampler is called, the corresponding texture image is a non-power-of-two image (as described in the Mipmapping discussion of section 3.7.7), and either the texture wrap mode is not CLAMP_TO_EDGE, or the minification filter is neither NEAREST nor LINEAR.

Kellby answered 1/8, 2017 at 12:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.