Archived

This topic is now archived and is closed to further replies.

What is Pitch?

This topic is 5009 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

When Lock the IDirect3DTexture9, it return a D3DLOCKED_RECT struct. There is Pitch member in D3DLOCKED_RECT, How to use it? We lock it because we want to get the pointer of the surface. why pBits is not enough?

Share this post


Link to post
Share on other sites
because not each pixel is directly behind the previous pixel in memory. this is to archive f.e. 32bit alignment, or 16byte alignment, or what ever alignment. for fast accessing of the data.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
The pitch is the size of each row of the texture. You cannot just use the pixel width as it may be bigger due to padding at the end.

So to access the pixel at 20, 50 you would do this

int index=20+(50*pitch)

NOT index=20+(50*width)

Share this post


Link to post
Share on other sites
Consider if you have a surface that is 3x3 pixels (assuming the card supports non-powers of two), and it''s pixel format is R8G8B8, thus 3 bytes per pixel, in memory, you would think that it is arranged like this:
RGBRGBRGB
RGBRGBRGB
RGBRGBRGB
where each "RGB" is one pixel, 3 bytes wide. So each row is 9 bytes wide. The memory address of the second row would be pBits + 9. But as the others have said, DirectX (and many other graphics libraries) try to align pixel data so that each row is a multiple of some number. Often, this alignment is 4 bytes, but it could be a lot of values, such as 1 byte or 16 bytes. If the previous example were aligned to 4 bytes, that means that each row needs to actually be 12 bytes wide. It would end up looking like
RGBRGBRGB...
RGBRGBRGB...
RGBRGBRGB...
Each "." is 1 extra byte, with probably a junk value in it. Because of this padding, you no longer can calculate the width of the row simply by BytesPerPixel x PixelsPerRow. Instead, you need to calculate the Pitch, and if you don''t know the alignment amount, then you can''t do this. Thus, DirectX tells you the alignment amount. And you don''t know the alignment amount because DirectX could change. Let''s say they were previously aligning to 4 bytes, as in the example above, but now they changed to 8 bytes. A row can''t be 9 byte, or 12 bytes wide anymore. It needs to be 16 bytes. Looking like this:
RGBRGBRGB.......
RGBRGBRGB.......
RGBRGBRGB.......
So DirectX just goes ahead and gives you the pitch, so that you can calculate the memory location of the next row using that.


int Agony() { return *((int*)0); }    Mwahaha... >8)

Share this post


Link to post
Share on other sites