[SDL] surface pitch

Sami Näätänen sn.ml at keijukammari.fi
Sat Jun 6 08:11:54 PDT 2009


On Saturday 06 June 2009 14:09:14 Paul Duffy wrote:
> --- On Sat, 6/6/09, Hubert Maraszek <marach5 at gmail.com> wrote:
> > Maybe that difference is because of SDL trying to make a
> > "bytes per scanline" amount a "round"
> > (in binary, of course) number? (in example: 1024, 640, 16,
> > 65536 etc.)
>
> You're almost right. I can't say if it's identical for PSP (actually,
> looking at the numbers where 272/4=68, almost certainly not) but on PC the
> graphics hardware will only accept data that is 4 byte aligned, that is 32
> Bit, so the data for every line must be a multiple of 4 bytes in size
> regardless of whether all that data is actually used. This can make memory
> transfers 4x faster than if individual bytes were accessed because it costs
> the same on PC architechture to move one byte as it does to move four as
> one 32 bit integer. Other colour depths will not automatically line up to a
> multiple of 4 and so need to be padded out on each line until they do.

In fact some platforms need the lines to be 64 bit aligned and for cache and 
SIMD reasons it is better to set the row alignment as 128 bits.

> What it comes down to is, sometimes to get graphics data aligned and
> displaying properly you have to pad each line out a bit and while the
> original width of an image needs to be recorded the padded width is also
> required to compensate for this padding when you want to access the correct
> pixel. So, while this does speed up things like blitting whole images you
> have to access individual pixels as:
>
> (y*pitch*BytesPerPixel)+x

This is wrong.

The start of row y is y*pitch bytes from the begining of the pixel data as 
pitch is equal to the amount of bytes between two rows of pixel data.

The correct one is y*pitch+x*bytesPerPixel

> Or, if you wanted to access the rightmost pixel on a line it could be
> (someone correct me if I'm wrong):
>
> (y*pitch*BytesPerPixel)+(width-BytesPerPixel)

This would be
y*pitch+(w-1)*bytesPerPixel





More information about the SDL mailing list