PocketMatrix forums

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

Double-buffering problem

Ok guys, anyone that saw my previous post knows this already, for the rest... I'm putting double-buffering into my modest (read: crappy!) little graphics library, but I'm facing a problem...

I've done the following during initialization:

Code:

unsigned short int* GX_pScreenBuffer = NULL;
GX_pScreenBuffer = (unsigned short int*)malloc(154000);


This is my back-buffer.

Now then, all I do is I draw my frame onto the back-buffer using what we would call standard GAPI drawing functions (i.e., I treat this pointer just like the pointer GAPI returns and I draw to it as such with the proper pitches and all).

Anyway, when the frame is drawn, I do the following:

Code:

unsigned short int* pVidMem = (unsigned short int*)GXBeginDraw();
memcpy(pVidMem, GX_pScreenBuffer, 153600);
GXEndDraw();


Now... on the emulator, using the GAPI DLL floating around the web, it works just fine, no problems whatsoever. Therefore, I'm assuming my basic technique is correct.

The problem is that when I execute this on my real Casio E-125, I get a blank screen, nothing at all. I'm totally at a loss to explain this, but I'm betting it's some absolutely stupid little thing I just don't know.

If anyone has the slightest clue, I'd appreciate hearing it because I'm completely stumped (not too tough to do these days).

Thanks all!
Code:
You have to be careful and take the pitch into account.

The horizontal byte stride of the Casio E-125 is actually 512 bytes (not 480!).

This means the actual size of the display memory is 320 * 512 bytes.
Ok, using 163840 (320*512) worked! Now, my question is this: the Y-Pitch returned by GAPI comes back as 256 on the E-125 (X-Pitch is 1). Therefore, I obviously do 320 * (Y-Pitch * 2) to determine by back-buffer size. Will this be the correct way to do it on all devices?

Secondly, I'm assuming there is a MUCH better way to do double-buffering than memcpy because my frame rate dropped from 32 to 18. I certainly expected some hit, but that's not going to cut it.

Any ideas?
On a StrongARM it should be feasible to perform double-buffering without memcpy. You should be able to allocate another buffer in physical memory and point the "LCD" DMA channel alternatly at that and the "Windows" video bank.

I got quite far investigating that, until I needed Platform Builder for header files, functions, etc (we do have an MSDN subscription here but we produce PC software and the idiot of a techie threw away everything that was vaguely useful ... I'm waiting 'til the next set with Platform Builder arrives, then I'll look at it again and report back)..

(You're taking more of a hit 'cos you're presumably copying the 320x512, some of which isn't even visible. Copy only what you need - I'm pretty sure that the screen isn't cached or buffered so reads and writes will be slooow).
Indeed, don't copy 320x512... Try copying 320x480 instead. Might be faster, might be slower... I will have to test this also.

The pitch returned by GAPI on a E-125 is 512, not 256. The pitch is always specified in bytes (not pixels). The correct size is pitchY * height (or 512 * 320).

Refractor:
I've been looking for ways to do real double-buffering for a long time and haven't found anything on the subject. If you have anything, that would be great!
By the way, you shouln't be allocating your own buffer on the E-125, otherwise performances are going to be really bad. The E-125 GAPI implementation is already providing you with a backbufffer (i.e. IsDisplayDRAMBuffer() will return TRUE).

So basically you should be rendering directly to the GAPI buffer and GXEndDraw() will take care of updating the actual display.
Kzinti:

I found no articles or anything on the subject. :cry:

However, I think that I have it figured out... I just need to get PlatformBuilder to complete it.

If you want me to do a "general theory" post saying how I think it can be done, let me know and I'll dig up my notes. :)
Well I understand how it can be done... Allocating some memory, find the physical address of this memory and put it in some video hardware registers...

Question is: where are these video registers and how do they work.
refractor wrote:
Kzinti:
If you want me to do a "general theory" post saying how I think it can be done, let me know and I'll dig up my notes. :)


Please do so :). I have PB, so if you want me to search for some specific header files, etc. just let me know.

Alternatively, would it be an option to take a look at the source code of a linux distro for StrongARM instead of getting nasty with PB?

cryo
Jasper · 13 Jun 2002, 10:00 UTC — Intel SA-1110
Kzinti wrote:
Well I understand how it can be done... Allocating some memory, find the physical address of this memory and put it in some video hardware registers...

Question is: where are these video registers and how do they work.


The LCD controller information appears in the SA-1110 (StrongArm) developers manual here:

http://developer.intel.com/design/pca/a ... 10_brf.htm

Seems fairly straightforward, but there may be implications running within PPC OS ?

Let us know if you do manage to figure it all out :)
I don't seem to have my notes here at work, so this is going to be largely from memory (no pun intended :)).


Well I understand how it can be done... Allocating some memory, find the physical address of this memory and put it in some video hardware registers...


Sort of. The problem is that if you ask Windows for some memory, the memory it gives you might be contiguous logically, but fragmented physically. That means that the DMA (using the physical memory addresses) would carry on regardless through memory you haven't filled (potentially).

Here goes:

Firstly, I'm currently assuming that the PPC devices using StrongARMs all use the on-board LCD display system (built in to the StrongARM itself).

If you look at the StrongARM reference manual, the LCD is "fed" with a high-priority DMA (takes the highest priority in the system, allegedly ;))

The LCD sub-system is controlled by a series of hardware registers that are mapped into the SA's physical address space. So, the first task, is to ask Windows to map that physical address space into your application's logical memory space for read/write access.

IIRC, the virtualAlloc and virtualCopy functions should be used for that (noting that only full pages of memory can be mapped in, not just individual words).

So, now you are able to read and write to the LCD DMA control registers. If you read the base address of the current buffer, hey-presto, that's the physical address of the RAM that Windows uses, and hence buffer #1 (with that buffer, you can generally get the logical address from GAPI or similar and not have to bring it into your address space yourself).

The next trick is getting buffer #2. First, you need to ask windows to grab a chunk of memory (big enough for the video buffer - including the extra pitch, of course). So, we use VirtualAlloc again, asking only to reserve a block of memory, not to actually allocate it.

We then try and lock the pages (LockPages?) and check through them to check that they're forming a contiguous set ... if they're not then we try again... and again .. and again, until we either get a contiguous block of physical memory, or we exhaust the memory and are forced to give up (remember to release the dis-continuous memory blocks at the end).

So, where are we? We have:

a) LCD DMA registers mapped into logical memory
b) The buffer #1 that Windows allocated (and its physical address).
c) A physical buffer the size of the screen, buffer #2

All we need to do now is:
Use VirtualAlloc/VirtualCopy to map buffer #2 into our address space so we can actually read/write it

Try shoving the address of our buffer #2 into the LCD DMA control register (more details about what needs to be done for that to work is in the SA reference manual).

Extra notes & provisos:

IIRC PPC2002 has a function that grabs a contiguous set of physical memory for you.

The physical memory we allocate for the buffer #1 (and the memory for buffer #0) is NOT cached OR buffered, so access to it is slow. If you're reading data back from the screen memory in your application a lot, it MAY still be faster to use a cached block and blit. If you're drawing without overdraw, you're skipping copying a full screen into a non-cached, non-buffered area per refresh - a significant saving.

C/C++ access *may* cause you real problems unless you can convince the compiler that the memory you're accessing is *volatile*. If it starts caching the LCD DMA registers in registers in routines you're going to be given the same value all the time even when the LCD registers have changed. I was going to just do things in ARM to solve that one, but there should be flags for it in C/C++.

This may "upset" the device, especially if Windows is doing strange things with the DMA. Ideally we should check that the LCD DMA interrupt isn't enabled... if it is, we need to know what it's doing. Obviously Windows is only aware of one buffer, so windows will draw there.

The application MUST, therefore, be VERY CAREFUL about cleaning up and putting the buffer #1 address back into the LCD DMA base register .

If the LCD DMA interrupt isn't hooked by Windows, then it might be a good plan to hook it ourselves so that it can perform the bank switching for us.

Final note:
Warning, if you try any of this, you're going to be playing with your device's internal configuration - if you get it wrong you could damage your device (we're writing to the LCD registers and LCDs are fussy). While testing it, I'd also EXPECT lots of hard-reboots to be necessary, so have nothing in your RAM that you value.

Don't blame me if this is wrong, or if you trash something using this information.
Wow refractor, awesome info. I knew this was possible; Jimmy used it for his games (at least that's what he wrote somewhere). Their tech guru (lost his name) also mentioned it once. We really need to get this stuff in our libraries (GapiDraw, Overloaded, EasyCE, PocketFrog, did I forget any? :) ), so that the developers can benefit without having to figure this out again. :)

Are you going to experiment with this further? If you can post sources to a working demo, that would be completely awesome. :)
Refractor,

Jasper wrote:
Seems fairly straightforward


I retract that bit of my last post, it wasn't that straightforward at all :)

Thanks for the REALLY clear explanation though, this may be worth trying to squeeze that last bit of extra performance out of the ARM devices.

I suspect that any implementation is going to need checking across all the major PPC devices though before it could be released generally...
I knew this was possible; Jimmy used it for his games

Well it's good to know that it can actually be done, at least :)

Yes, I fully intend to play with it more when I have a bit more time, and I get my grubby paws on the PlatformBuilder bits and pieces. I've forgotten which header files I need exactly (I'll look at my notes/code tonight). Does the PlatformBuilder have more general information about the low-level side of Windows CE? The MSDN set I have isn't at all clear about the interrupt management, etc. :(

I retract that bit of my last post, it wasn't that straightforward at all

If you understand the differences/links between logical and physical memory, and that the DMA needs a contiguous block, it's fairly straightforward :) The application works in logical space, and the registers/DMA are physical.

Thanks for the REALLY clear explanation though

No problem. :)

You're right about the testing. I can only really test it on an iPAQ 3630, 'cos that's all I have. I need to know more about how other devices are configured before I could provide code for them (largely the actual pitch of the windows frame buffer).

Cheers,
Refractor
fzammetti · 13 Jun 2002, 16:37 UTC — IsDisplayDRAMBuffer()?
Ok, so now my thinking is this:

I currently have a function as follows:

Code:

void GX_beginDraw() {
GX_pScreenBuffer = (unsigned short int*)GXBeginDraw();
}


(I do this just so that all the function calls in my library are consistently named). The rest of the library functions write to GX_pScreenBuffer.

As you would guess, I also have:

Code:

void GX_endDraw() {
GXEndDraw();
}


Anyway, to do double-buffering, all I've done is remove the code from GX_beginDraw (GX_pScreenBuffer is always pointing to the chunk of memory I allocated during initialization that is my back-buffer). Then, during GX_endDraw, I simply do:

Code:

memcpy(GXBeginDraw(), GX_pScreenBuffer, 163840);


This works just fine, albeit slowly, and should fix my flickering problems on the 36xx iPaq series.

From reading what you guys wrote, it seems that on my E-125, I am essentially getting triple-buffering since GAPI is doing it for me underneath what I'm doing.

So, it seems all I need to do is do a call to GXIsDisplayDRAMBuffer() during my GX_beginDraw() function, and if it returns true, then I do GXBeginDraw() there, setting GX_pScreenBuffer to what GXBeginDraw() returns me.

Then, I also do a call to IsDisplayDRAMBuffer() in my GX_endDraw() function. Again, if it returns true, I just call GXEndDraw() and I'm happy. Otherwise, I do the memcpy myself. Does this sound close to right?

Another question: I still don't quite understand why:

Code:

GXDisplayProperties GX_g_gxdp = GXGetDisplayProperties();
GX_xPitch = GX_g_gxdp.cbxPitch >> 1;
GX_yPitch = GX_g_gxdp.cbyPitch >> 1;


I get GX_xPitch == 1 and GX_yPitch == 256. From what I read in this thread, it seems GX_yPitch should be 512, but it's not returned that way. Am I just being extrodinarily dense?

Thanks again folks!
The pitch is returned as 512, but since you are dividing it by two (the logical shift), you end up using 256. This should be fine unless some weird display comes up with an odd pitch value (would be really surprising).
See, it's always something obvious that you miss! If you need me, I'll be over in the corner, KICKING MYSELF!
Hm, hate to say this, but that was REALLY dumb. :)
Before we get the "don't bash other coders" discussion, that was a joke. I make mistakes like that all the time. ;)
Hi.

If somebody would be so kind as to zip up together and mail me the:

pkfuncs.h
and
Nkintr.h

files from the MS Platform Builder, I'd be really grateful (if they include other headers that I might not have, I'd appreciate them too, please).

If it gets to me within the next few hours I can look at things a bit this weekend while going to and from Switzerland. :)

Cheers,

Refractor

(therefractor@hotmail.com)
Got them now. Thanks very much :)

(Won't say who sent them in case MS looks upon this as "naughty").
Actually, the one group of people that Microsoft tends to NOT harass is developers. They tend to treat us well, so I wouldn't worry too much.

(Then again, it *IS* Microsoft we're talking about, so better safe than sorry I suppose!)