Direct State AccessGL 4.5

glTextureSubImage2D

Uploads pixels into a region of an existing texture by ID.

Part of the family glTexSubImage*

Signature

void glTextureSubImage2D(GLuint texture, GLint level, GLint xoffset, GLint yoffset,
                         GLsizei width, GLsizei height,
                         GLenum format, GLenum type, const void *pixels);

How it works

The usual partner of glTextureStorage2D: storage first, pixels second. It copies a width × height rectangle of your data into one mip level, at (xoffset, yoffset) counted from the bottom-left. format and type describe your data, not the texture: GL_RGBA + GL_UNSIGNED_BYTE for an 8-bit image from stb_image.

After uploading level 0, call glGenerateTextureMipmap to fill the smaller levels.

Parameters

textureGLuint

Target texture (already allocated).

levelGLint

Mip level to write.

xoffsetGLint

Left edge of the region.

yoffsetGLint

Bottom edge of the region.

widthGLsizei

Region width.

heightGLsizei

Region height.

formatGLenum

Channel layout of your data: GL_RGB, GL_RGBA, GL_RED…

typeGLenum

Channel type of your data: GL_UNSIGNED_BYTE, GL_FLOAT…

pixelsconst void *

Source pixels.

Tips & pitfalls

  • Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.

Example

example.cpp
1glTextureSubImage2D(tex, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Official Khronos documentation