Is glTexStorage2D required when creating mipmaps automatically

I am updating a 2D texture in OpenGL 4.0 with gltexSubImage2D (). Texture has mipmaps automatically created using

glGenerateMipmap(GL_TEXTURE_2D); 

My texture update fails until I realized that I had to update mipmaps as well when updating (or remove mipmaps generations). Then I read this wiki where glTexStorage2D is used when creating mipmaps.I actually never paid attention to this method. So I wonder if I need to use this every time I create a texture with mipmaps?

Update:

The method shows that he

defines storage requirements for all levels of a two-dimensional texture or one-dimensional texture array at the same time

I assume that using this should improve performance when creating mipmaps? It's true?

+4
source share
2 answers

Before glTexStorage appeared, each mipmap level had to be initialized with a separate call to glTexImage. glTexImage triggers many complex internal state changes, which are quite expensive. Each time glTexImage is called, the layout of the texture object changes.

Therefore, you try not to use it, if possible. As you have discovered, you must first initialize all the desired mipmap levels before you can update them with glTexSubImage. glGenerateMipmap will create them that make changes to the layout, which, yes, is expensive.

glTexStorage initializes the entire texture object once for the desired, immutable format. Yes, using glTexStorage improves performance when sharing glGenerateMipmap or updating data using glTexSubImage, but improvements come very early when creating texture objects and not so much when changing its data.

+7
source

Two functions are fully orthogonal.

Choose between glTexImage2D and glTexStorage2D . Both of these provide storage for texture images ; they just do it differently.

glTexImage2D is an old way of allocating memory. It creates mutable storage (there are other functions that also create mutable storage) . If you want mipmaps, you must allocate each mipmap level with a separate function call, manually calculating the size of the mipmap image (see below for an exception).

glTexStorage2D allocates an immutable image data storage . It selects all the mip cards that you want all at once.

Immutable storage, as the name implies, cannot be changed. See, you can call glTexImage2D at the same mipmap level, with a different size. And if you do, OpenGL will destroy the original mipmap level and allocate storage for the new one. You can even change the formats.

Immutable storage will not let you change anything. You can load new data using functions such as glTexSubImage2D . But these functions do not change the repository; they change the contents.

glTexImage2D and glTexStorage2D are similar to malloc . glTexSubImage2D is similar to memcpy ; It only works for existing memory.

glGenerateMipmap is a function that will take the base texture layer and generate data for all the mip cards from this base layer (given the mipmap range) . He does it once. Think of it as a rendering function; he does his work and then does it.

For mutable storage textures, glGenerateMipmap also allocates storage for any mipmaps in the mipmap range that were not previously allocated. This is why you can call glTexImage2D once, then call glGenerateMipmap to make it do all the other mipmaps without having to do it manually.

+23
source

Source: https://habr.com/ru/post/1469186/


All Articles