4
$\begingroup$

I'm trying to create single-channel (no mipmaps) texture initialized with my custom pixel data, but it crashes on glTextureSubImage2D. Any idea why?

const std::vector<GLubyte> myData(1025 * 1025, 0);
GLuint texture;
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
glTextureStorage2D(texture, 1, GL_R8, 1025, 1025);
glTextureSubImage2D(texture, 0, 0, 0, 1025, 1025, GL_RED, GL_UNSIGNED_BYTE, &myData[0]);
$\endgroup$

1 Answer 1

5
$\begingroup$

Pixel transfer operations are governed by more than just your parameters to a pixel transfer function. They are also governed by parameters passed to the glPixelStore functions.

Of particular interest to you is GL_PACK_ALIGNMENT. This tells OpenGL what the byte alignment for each row of data is in your array. By default, this value is four, not one.

This means that OpenGL will believe that the second row of your image starts at this address: &myData[1028], since 1025 is not aligned to 4 bytes. And that means it will run off the end of your allocated buffer eventually.

If you are using data with 1-byte alignment for rows, you must inform OpenGL of this by calling glPixelStorei(GL_PACK_ALIGNMENT, 1);. Note that you should not do this by default; do it only when dealing with byte aligned data.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.