If you were using glTexImage2D (...)
to allocate storage for your texture, it would be possible to re-allocate the storage for any image in the texture at any time without first deleting the texture.
However, you are not using glTexImage2D (...)
, you are using glTexStorage2D (...)
. This creates an immutable texture object, whose storage requirements are set once and can never be changed again. Any calls to glTexImage2D (...)
or glTexStorage2D (...)
after you allocate storage initially will generate GL_INVALID_OPERATION
and do nothing else.
If you want to create a texture whose size can be changed at any time, do not use glTexStorage2D (...)
. Instead, pass some dummy (but compatible) values for the data type and format to glTexImage2D (...)
.
For instance, if you want to allocate a texture with 1 LOD that is m_width
xm_height
:
glTexImage2D (m_target, 0, m_format, m_width, m_height, 0, GL_RED, GL_FLOAT, NULL);
If m_width
or m_height
change later on, you can re-allocate storage the same way:
glTexImage2D (m_target, 0, m_format, m_width, m_height, 0, GL_RED, GL_FLOAT, NULL);
This is a very different situation than if you use glTexStorage2D (...)
. That will prevent you from re-allocating storage, and will simply create a GL_INVALID_OPERATION
error.
You should review the manual page for glTexStorage2D (...)
, it states the following:
Description
glTexStorage2D specifies the storage requirements for all levels of a two-dimensional texture or one-dimensional texture array simultaneously. Once a texture is specified with this command, the format and dimensions of all levels become immutable unless it is a proxy texture. The contents of the image may still be modified, however, its storage requirements may not change. Such a texture is referred to as an immutable-format texture.
The behavior of glTexStorage2D depends on the target parameter.
When target is GL_TEXTURE_2D, GL_PROXY_TEXTURE_2D
, GL_TEXTURE_RECTANGLE
, GL_PROXY_TEXTURE_RECTANGLE
or GL_PROXY_TEXTURE_CUBE_MAP
, calling glTexStorage2D is equivalent, assuming no errors are generated, to executing the following pseudo-code:
for (i = 0; i < levels; i++) {
glTexImage2D(target, i, internalformat, width, height, 0, format, type, NULL);
width = max(1, (width / 2));
height = max(1, (height / 2));
}
When target is GL_TEXTURE_CUBE_MAP
, glTexStorage2D is equivalent to:
for (i = 0; i < levels; i++) {
for (face in (+X, -X, +Y, -Y, +Z, -Z)) {
glTexImage2D(face, i, internalformat, width, height, 0, format, type, NULL);
}
width = max(1, (width / 2));
height = max(1, (height / 2));
}
When target is GL_TEXTURE_1D
or GL_TEXTURE_1D_ARRAY
, glTexStorage2D is equivalent to:
for (i = 0; i < levels; i++) {
glTexImage2D(target, i, internalformat, width, height, 0, format, type, NULL);
width = max(1, (width / 2));
}
Since no texture data is actually provided, the values used in the pseudo-code for format
and type
are irrelevant and may be considered to be any values that are legal for the chosen internalformat
enumerant. [...] Upon success, the value of GL_TEXTURE_IMMUTABLE_FORMAT
becomes GL_TRUE
. The value of GL_TEXTURE_IMMUTABLE_FORMAT
may be discovered by calling glGetTexParameter with pname
set to GL_TEXTURE_IMMUTABLE_FORMAT
. No further changes to the dimensions or format of the texture object may be made. Using any command that might alter the dimensions or format of the texture object (such as glTexImage2D or another call to glTexStorage2D) will result in the generation of a GL_INVALID_OPERATION
error, even if it would not, in fact, alter the dimensions or format of the object.