We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3034 - SDL_UpdateTexture trashes GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT
Summary: SDL_UpdateTexture trashes GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT
Status: RESOLVED WONTFIX
Alias: None
Product: SDL
Classification: Unclassified
Component: video (show other bugs)
Version: 2.0.3
Hardware: x86_64 Linux
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-24 16:38 UTC by Dave
Modified: 2015-06-27 02:30 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dave 2015-06-24 16:38:12 UTC
There is a gotcha I ran into when mixing SDL's openGL interface with direct openGL calls to glTexImage2D.

libsdl adjusts the state variables GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT when converting pixel data in memory to textures inside the SDL_UpdateTexture function.

The trouble is, the library doesn't restore those to default values. Online searches for example code that makes use of glTexImage2D don't explicitly set those values, particularly GL_UNPACK_ROW_LENGTH. So when you cut and paste supposedly working code into your project, it won't work out of the box. Incorrect GL_UNPACK_ROW_LENGTH (which is default 0 in openGL's state) can cause seg faults, or at the very least textures will be skewed in some way.

The safe solution is to set GL_UNPACK_ROW_LENGTH and GL_UNPACK_ALIGNMENT to their default values before using glTexImage2D:
	glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
or, set them to whatever is necessary at the time. It is unsafe to assume they are set to default values.

I don't think it's reasonable to expect libsdl to restore those variables to default values. This is just some detail programmers need to be aware of. I'm entering this as a bug because I'd like somewhere for there to be a warning about this issue in the libsdl documentation.

I wasted some time tracking down the cause of the problems I was experiencing, and had to examine the libsdl source to get a hint of what was going on. I had never made use of those GL_UNPACK_* variables, wasn't aware of their existence. Hopefully other people can benefit from my experience.
Comment 1 Sam Lantinga 2015-06-27 02:30:12 UTC
Feel free to send a note to the SDL mailing list so people can see the information more easily. If you are not subscribed your mail will be held for moderation, and just send me e-mail so I know to look for it among the spam.

Thanks!