Optimal pixel alignment

Started by
2 comments, last by clapton 18 years, 10 months ago
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!
___Quote:Know where basis goes, know where rest goes.
Advertisement
<silence> Hmmm ... Maybe I mistook something? ;) If so, please let my know as well. ;)
___Quote:Know where basis goes, know where rest goes.
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).
struct{    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.
Thank you. :) It's good to know that it's mostly a graphics driver who takes care of the memory alignment.
___Quote:Know where basis goes, know where rest goes.

This topic is closed to new replies.

Advertisement