🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

<<HELP a newbie>> Wanna explain memory pitch?

Started by
5 comments, last by Gollum 24 years, 6 months ago
Phew! I am very new. Learning about C++ and directx @the same time. Readin Lamothe''s latest book, and I''m up to creating surfaces with ddraw4. He has completely lost me with the idea of memory pitch. If anyone cares to give me a relatively high-level explanation of what memory pitch is, I''d be much obliged. - Chris (Who is reading every chapter about three times to understand it - sort of.) :-)
Advertisement
If your refering to screen pages in memory then the pitch is just the actual length of a scanline.

Let me ''splain
say you are working with 320 X 240 resolution so to find any given pixel you would use (320 * row) + column, but that would only work in rare cases because in memory this screen might be 400 X 240 , so that it would fit better in memory (and for other reasons as well) thus you have a screen width of 320 but a pitch of 400:

/---------320---------/ = screen width
/--------------400------------------/ = pitch
/ / /
/ / /
/ visible screen / extra mem /
/ / not visble /
/ / /
-------------------------------------

so to find a pixel in your visible screen you have to take the pitch and not the widht into consideration, for example (pitch * row) + column
hehe wow those funky lines are actuall boxes when you use your imaginations... sorry

-b
The pitch is the number of bytes in your screen width. In 320x240x8 your pitch is 320 bytes. In 640x480x16 it is 1280 bytes (2*640)
Well actully in directx pitch can be differnt than just the screen res. Thats why you just multiply by it when you lock a surface. Theres no guarrantee that it will be a certain number,and it can change between locks.(or atleast for videomemory for system im not sure.)
If anyone is interested in the reason for this, it is because some video cards use padding at the end of eachline or in certain color depths, as certain opertions are faster on byte lengths of multiples of two.

For example some drivers store each 24bit pixel in 4 bytes (32bit) because it is an easier number to deal with. The extra 8 bits are simply ignored.

e.g

/-------------------------------/-------/
*screen width* *padding*

In this respect Anonymous Poster was right in his original answer, but I thought it required a bit more explanation.

Edited by - vallis on 1/5/00 3:26:17 AM
Ok, you guys are gonna laugh, but it finally clicked for me yesterday. What I wasn''t getting was the idea of pixels in a 2d surface being addressed in a linear fashion. I was thinking of pixels as points with an (x,y) address just like in geometry class. That''s why the formula [x + y*pitch] didn''t make sense to me.

Thanks for your help. Ah, it''s always fun being a newbie.

- Chris "Hey, I made a blank screen! Yippee!" McCormick

This topic is closed to new replies.

Advertisement