TexturesGL 4.2

glTexStorage2D

Allocates immutable storage for all mip levels of a 2D texture at once.

Signature

void glTexStorage2D(GLenum target, GLsizei levels, GLenum internalformat,
                    GLsizei width, GLsizei height);

How it works

The modern way to allocate a texture. You declare size, format and number of mip levels once; they can never change afterwards (the contents can). Knowing this upfront lets the driver skip completeness checks, and it prevents a whole class of "texture is incomplete" bugs.

Pixels are uploaded afterwards with glTexSubImage2D.

Parameters

targetGLenum

GL_TEXTURE_2D or GL_TEXTURE_CUBE_MAP.

levelsGLsizei

Number of mip levels. Full chain: 1 + floor(log2(max(w, h))).

internalformatGLenum

A SIZED format: GL_RGBA8, GL_SRGB8_ALPHA8, GL_RGBA16F, GL_DEPTH_COMPONENT24… (unsized GL_RGBA is rejected).

widthGLsizei

Width of level 0.

heightGLsizei

Height of level 0.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GL_INVALID_OPERATION
The bound texture already has immutable storage.
GL_INVALID_ENUM
internalformat is unsized.

Example

example.cpp
1glBindTexture(GL_TEXTURE_2D, shadowMap);
2glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 2048, 2048);
Official Khronos documentation