glTexSubImage2D strange behavior

I’m trying to update a small portion of a larger image with glTexSubImage2D, but it appears corrupted somehow. I’ve already verified the STL vector storing the data has the correct data in it. I’ve narrowed the errors down to the glTexSubImage2D call, which looks like this:

glTexSubImage2D(GL_TEXTURE_2D, 0, xoffset, yoffset, width, height, GL_RGB, GL_UNSIGNED_BYTE,;

I’m trying to update a square of 5x5 texels, so width and height are both 5. The points to a STL vector of GLubyte values, and has the correct values (all the same). But when I try to paint an orange square, it comes out like this:

In wireframe:

There is some of the correct color there, but it’s definitely reading something wrong.

Interestingly, when I change width to 4, it works as expected, aside from only updating 4 texels in the x direction:

I don’t understand why this would be, since the actual data is the same in both directions, and has 25 elements total. glGetError returns GL_NO_ERROR.

In case it’s needed, the code to create the texture:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE,;// All black

Any ideas? It seems like a simple thing I’m overlooking, but all the dimensions of all the vectors seem correct.

This is normal, documented behaviour. A 5-wide GL_RGB GL_UNSIGNED_BYTE texture is 15 bytes wide, which is not a multiple of 4; you need to set your GL_UNPACK_ALIGNMENT via glPixelStorei and a value of 1 is required in this case; please see for more information. This is also why a value of 4 works (4x3=12 which is a multiple of 4).