Advertisement Jump to content
Sign in to follow this  

Optimal pixel alignment

This topic is 4983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I've heard that different platforms work more efficiently with some particular buffers' organization. Are there any rules concerning that? How can I find the optimal configuration? Thanks in advance!

Share this post

Link to post
Share on other sites
Its not pixel alignment, its memory alignment.
The graphics card can work with data more efficantly if it is aligned on certain boundries.
For textures and the framebuffer this is handled automatically by the driver and you dont have to worry about it.
The main area you have to keep an eye on things is with vertex arrays, where you dont want to let your sections of data become misaligned as this makes more work for the GPU/memory bus to sort it out. Generally you want to keep things aligned to the size of a float (4 bytes).

GLfloat x,y,z;
GLfloat u,v;
GLubyte r,g,b;
} vertex;

The above is an example of how not todo it. Everything is fine for the x,y,z,u & v values as each one takes up 4 bytes and thus remains aligned, the problem starts at the r,g & b varibles.
Each of those is 1 byte and while the GPU can read these values just fine the 3 varibles sum to only 3 bytes, which means the next data in a closely packed array will start one byte misaligned, causing the GPU/data bus some 'issues', including reduced performance.

There are two ways to fix this problem, one is to convert your r,b & b varibles to GLfloats, this fixes the alignment but wastes resouces. The other is to simply include an extra GLubyte, either as a dummy or as an alpha value, which will even things out.

Granted, there is a chance this might not happen at all, the compiler could well pad that structure out to keep it aligned and all would be good anyways, however its something worth keeping in mind when declaring data.

Share this post

Link to post
Share on other sites
Thank you. :) It's good to know that it's mostly a graphics driver who takes care of the memory alignment.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!