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
Target texture (already allocated).
Mip level to write.
Left edge of the region.
Bottom edge of the region.
Region width.
Region height.
Channel layout of your data: GL_RGB, GL_RGBA, GL_RED…
Channel type of your data: GL_UNSIGNED_BYTE, GL_FLOAT…
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
1glTextureSubImage2D(tex, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);