PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Proper way to determine back-buffer size

Ok, I'm sooo close now...

On devices where GXIsDisplayDRAMBuffer() returns false, you want to do your own double-buffering. Fine, good.

My question is this: what is the PROPER way to calculate the size of the memory chunk to allocate for the buffer?

I'm currently doing the following:

Code:

GXDisplayProperties GX_g_gxdp = GXGetDisplayProperties();
GX_xPitch = GX_g_gxdp.cbxPitch >> 1;
GX_yPitch = GX_g_gxdp.cbyPitch >> 1;
GX_bufferSize = (320 * (GX_yPitch * 2)) + 500;
GX_pScreenBuffer = (unsigned short int*)malloc(GX_bufferSize);


(I left out all the conditional stuff, this is the relevant part I think)

I add 500 just for safety, and because it seemed like not doing it I was having some problems on my E-125, so I figured a little extra wasn't going to hurt very much.

However, this strikes me as being the wrong way to calculate it anyway. It occurs to me that some devices, iPaqs I think, return -1 as their yPitch, so it doesn't seem like it's going to work in that case.

Can anyone show me the right way to do it (or tell me I in fact got lucky and am ALREADY doing it right??)

Thanks very much!
Not completely related, but to divide by 2 you used a shift. So, why not use a shift to multiply by 2 instead of using *2. I find that it is best not to rely on the compiler to optimize these things.
fzammetti · 14 Jun 2002, 21:06 UTC — Yep
I agree completely, and in fact I specifically go through all my code looking for optimizations like that all the time at regular intervals because, as you said, I don't trust the compiler that much. The code you see here is a first-pass, so I'm more concerned with it working and being right than totally optimized, and I always have found not using shifts to be easier to read and comprehend quickly, which is more important when I'm trying to get something working the first time through.
well, if all you want is the a buffer that you can use, do this.

Code:
UINT16 *backbuffer=malloc(320*240*2);//Yes, I left out the shift, but I'm going for readablility.


320 for the width (assuming landscape)
240 for the height (assuming landscape)
2 because you are dealing with 16 bit color.

What you could do to make it more universal is this

Code:
UINT16 *backbuffer=malloc(320 * 240 * GX_g_gxpd.cbxpith);


in this case GX_g_gxpd.cbxpith should be 2 on a 16 bit arm device (not sure about 12bit IPAQ, never developed for one). I am not sure why you are adding 500, but if that is extra space for something else, then just add it to one of the lines above.
angedelamort · 14 Jun 2002, 21:39 UTC
You should go read this topic:
http://forums.pocketmatrix.com/viewtopic.php?t=6099

Because the buffer on the casio is larger than 320x240 :)
(320*240*2);//Yes, I left out the shift, but

Don't panic - just about any compiler will replace this with 153600 long before it hits the assembler ;)

You are wise to go for the readability option in this case! You can even do a floating point or integer divide as long as it can be calculated at run time.
Yes Angel, that's the thread I started that got me wondering about all this... It sounded like, from reading that, that 320 * yPitch would work, but I believe yPitch comes back as -1 on an iPaq (I might be wrong about that). If that's the case, that doesn't seem like it will work at all.

I don't feel like I have a handle on this yet, it sounds like there is some conflicting info floating around...

How about one of the guys that wrote one of the libraries, or Dan certainly... how do you guys determine what size to make your back buffers? I'm assuming there must be a calculation that works across all devices but maybe that's a bad assumption... maybe you guys just look at the device type and you hard-code a size for each?

Ok, lemme go pull my hair out a bit more...
PocketFrog doesn't allocate a backbuffer when GXIsDisplayDRAMBuffer() returns true. This you understand.

For other devices, I allocate a buffer of width*height*2. Notice that I don't use the pitchs to calculate the size of this buffer.

Be careful when blitting though, as GXBeginDraw() doesn't return the start of the frame buffer but instead the top-left of your device's screen.

Another thing is the pitch of your device might be different then the one of your buffer. To find the actual pitch of the device, I use:

pitch = max( abs(gapi.xpitch), abs(gapi.ypitch) )
Hi fzammetti,

The size of the backbuffer is always width*height*(bytesperpixel). You should not allocate the backbuffer based on the pitch values since they change between devices. Also, the "extra" space between the 240 pixel wide Casio screen and the buffer width of 512 bytes could be used for something else on some future device - so it's best not to write into this area.

The tricky part is to get your backbuffer to the display. Basically there are two ways to do it:
(1) Rotate your backbuffer to the display based on the pitch values.
(2) Store the backbuffer aligned (rotated) to the display and rewrite all your drawing operations.

What way to go for has been discussed in detail on these forums. Basically (1) causes tearing and (2) may require more work in the drawing operations. Currently PocketFrog, GapiTools and GapiDraw use the second approach (2).

/Johan
fzammetti · 15 Jun 2002, 17:42 UTC — Maybe, finally...
Ok, after a ton of reading, research and some experimentation and trying to sort through all the information you folks have given me, I've reached what I think is my final answer...

To propererly allocate my back-buffer, I will use the following code to determine the size (and the size to use in my memcpy obviously):

Code:

GXDisplayProperties GX_g_gxdp = GXGetDisplayProperties();
signed long int GX_xPitch = GX_g_gxdp.cbxPitch >> 1;
signed long int GX_yPitch = GX_g_gxdp.cbyPitch >> 1;
unsigned long int GX_bufferSize = 320 * max(GX_xPitch, GX_yPitch) * (GX_g_gxdp.cBPP / 8)

(And let's ignore the shift vs. * thing this time! :) )

On my Casio, I know that doing 320*240*2 does not work, it needs to be 320*256*2, which is what you come up with using that formula (I know, I shouldn't be doing my own buffering on the Casio, and my code in fact does not, but I'm using it as an example).

On an iPaq, which my best research indicates has an xPitch of 640 and a yPitch of -2, that formula will work out to 320*240*2, just as Kzinti suggested.

(Incidentally, I know there is another thread trying to start a database of GAPI properties... any progress on that? It would have been helpful here)

So, it seems like that is the right formula across all devices (at least any I care about). Anyone see any obvious flaws there?

In addition, all of my drawing code will work with the pitches properly (i.e., just like all the sample GAPI code you see on the net, so I should be drawing into my buffer taking the device's pitch into account), so I should just simply have to do a memcpy from the buffer to actual video memory, no rotation worries or anything (this I'm a little less sure on, but I think it's the case).

I've already verified this works on my Casio just fine, so now my question is if it works on iPaqs, since they seem to be the sticking point anyway. IF anyone is willing, would you go to:

http://www.omnytex.com/GXLibTestV2

And give that a try? I'm just concerned with if it works at all on iPaqs for now, speed and such I can deal with later.

Thanks guys, I very much appreciate everything!
Applewood has a list of different screenmodes:
http://www.applewoodhouse.com/device.html

I have a link to a more detailed list somewhere, but i can't find it right now...
angedelamort wrote:
You should go read this topic:
http://forums.pocketmatrix.com/viewtopic.php?t=6099

Because the buffer on the casio is larger than 320x240 :)


Make sure when you say casio, you say non arm casio, since I have an e200, and have not noticed any blitting problems due to a buffer larger than the screen.