PocketMatrix forums

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

Optimizing GAPI

   Hi.  The name's Brandon Dillon, and I'd like to say "hi" to everyone.  I guess I already did that, but anyway, I've been working on developing a game for the PocketPC for a few weeks now, (It's an RPG, but we're just laying down the fundamental code right now) and while I've really enjoyed the development platform, (I was recently developing some Game Boy Advance code, but doing that freelance can be quite a challenge) I do have some questions that I'd like to ask.

   First, let me say a bit about the project.  It's a isometric tile-based RPG with turn-based combat.  All of the code written so far is completely hand-written.  (I avoided EasyCE and others because I wanted to understand the inner workings of the program)  I'm using GAPI and currently have graphics and input working quite well.  I've abstracted everything away from windows and have my own input handlers, image surface systems, and so on and so forth.  Right now, the only thing the application really does is scroll a map around, but I feel that I've got the capability to do quite a bit with the code.

   The nature of my questions are not how to acheive some particular task or anything like that; it's more a question of optimization.  My program runs quite well in the emulator (50 fps) and never experiences a slowdown from that benchmark.  Of course, this is not the case on my actual device. (iPaq 36xx)  I get roughly 8 frames per second, which isn't adequate for my needs.  Considering that it appears that I can run Quake on my iPaq at a faster framerate than that, I don't think I can blame it on hardware.  I've tried to search the boards and find a topic on optimization, but I couldn't seem to find any, so I decided to post a fresh topic.

   One of the concerns I have is that I'm blitting this map full-screen without any kind of virtual keypad or anything like that, and I'm updating the state as fast as the system will let me.  I'm moving my pre-loaded map buffer over to the back buffer, which is in turn moved over to the actual video memory at update-time.  Is there any way to avoid this overhead?  Also, my input system seems to lag the system heavily, but I can't seem to track down any specific conflicts within the system that would account for the slow-down.  Another concern is my image blitting code itself.  I am working on some optimizations for this myself, but if anyone has any insight, I'd love some advice.

   I, of course, would appreciate any tips on optimizing for the PocketPC in general that can only be acquired through experience.

   Let me say thank you in advance to anyone who helps out.  I came to this forum because it seems to be heavily trafficked by prominent and intelligent coders, and if you don't mind, I'd like to leech off of some of your experiences.

  Oh, and happy new years!

-Brandon DillonLast modification: bdillon - 01/01/02 at 00:12:24
First thing you need to do is find the bottleneck in the code you are executing each frame.  Unfortunately, there isn't a profiler available with eVC, but you should be able to narrow things down by bracketing sections of your code with calls to GetTickCount() or QueryPerformanceCounter().  You can also comment out the code that performs the back buffer to video memory copy then see how fast your code runs.  If it speeds up a lot then you know the bottleneck is this copy routine.  Just keep eliminating things until your performance jumps up.

The secret to fast blt performance on the iPaq devices is to create a back buffer that is oriented the same as the iPaq's display memory (landscape).  This makes the copy operation very simple as it a single call to memcpy.  The same thing goes with whatever you're blitting to the back buffer.  Make it so that you won't have to perform a rotation when you blit.

Of course if you're using palettes, colorkeys, or alpha blending during your blits, then those things will take more time as they aren't a simple memcpy, but we've discussed those things to death here and how to optimize them.  Search the archives or look at some of Jacco's tutorials for details.

Welcome to the fray.  I hope you'll find something useful here that will help you with your upcoming game.  Let us know how it goes and don't hesitate to ask for help or suggestions.

Try taking a look at the Easy CE and Pocket Quake sources (vid_ppc.c and sys_win.c) . Neither is derived from the other, so they provide different solutions to the same problems.

It is difficult for us to provide generalized solutions to your specific problems without seeing the source. Perhaps you could post excerpts of the critical loops. I'm sure you're aware you shouldn't be Sleeping anywhere in your message loop unless your app has lost activation, etc.

Dan East
Thanks for the help.  I did some testing and recoding for the larger part of the day, and it appears that my bottleneck was in my blitting engine.  I did a lot of tweaking, such as making the default view landscape, as reccomended, and then spent a lot of time re-writing the code to blit my surfaces.  After a few different versions, I settled on the ones that were giving me the best framerate. (~24 fps on a retail build, ~14 on debug)

The function that appeared to have the most overhead was my function to blit clipped images to the back buffer.  I'll post, it, but I'm warning you: it's ugly :)
Code:

void GBlitSurfaceClipped(Surface surface, Coord2i destCoord, Rect4u srcRect, bool alpha) {
     if(s_orientation == ORIENTATION_RIGHT) {
             Coord2u clipDims = {srcRect.right - srcRect.left, srcRect.bottom - srcRect.top};
           pixel * buffer = s_bBuffer + destCoord.y * SCREEN_WIDTH + destCoord.x;
           for(unsigned y = 0; y < clipDims.y; ++y) {
                 for(unsigned x = 0; x < clipDims.x; ++x) {
                       if(!(destCoord.x + x < SCREEN_WIDTH && destCoord.y + y < SCREEN_HEIGHT)) {
                             buffer += clipDims.x - x;
                             break;
                       }
                       buffer++;
                       pixel color = *(surface.ptr + (srcRect.top + y) * surface.dims.x + srcRect.left + x);
                       if(alpha && color == KEYFRAME)
                             continue;
                       *buffer = color;
                 }
                 buffer += SCREEN_WIDTH - clipDims.x;
           }
     } else {
           Coord2u clipDims = {srcRect.right - srcRect.left, srcRect.bottom - srcRect.top};
           pixel * buffer = s_bBuffer + (SCREEN_HEIGHT - destCoord.y) * SCREEN_WIDTH + (SCREEN_WIDTH - destCoord.x);
           for(unsigned y = 0; y < clipDims.y; ++y) {
                 for(unsigned x = 0; x < clipDims.x; ++x) {
                       if(!(SCREEN_WIDTH - destCoord.x - x > 0 && SCREEN_HEIGHT - destCoord.y - y >0)) {
                             buffer -= clipDims.x - x;
                             break;
                       }
                       buffer--;
                       pixel color = *(surface.ptr + (srcRect.top + y) * surface.dims.x + srcRect.left + x);
                       if(alpha && color == KEYFRAME)
                             continue;
                       *buffer = color;
                 }
                 buffer -= SCREEN_WIDTH - clipDims.x;
           }
     }
}

The two types (Coord2u and Rect4u) are just structs containing 2 and 4 unsigned values, respectively. They're good for coordinates and rectangles, too. ;-)
I was doing a lot of stupid stuff in the original version, like having a separate function blit every pixel by taking in coordinates and doing something like:
*(s_bBuffer + coord.y * SCREEN_WIDTH + coord.x) = color;
and, of course, the multiply was killing me per pixel.
If you see any flaws in the new function or have any reccomendations, _please_ let me know.  I won't get mad if you tell me my code sucks. (Okay, maybe just a little, but I'll try to be objective)  I tested most of the other parts of my engine, and all seem to be relatively optimized - most of my changes were in the graphics module.  I'm glad to know how the video memory works now.  Thanks for your help!
I would like to pose another question: does 24 fps seem reasonable on an iPAQ 3670 for a full-screen blit every frame?  I'm currently not doing any game-based calculations like pathfinding and I don't process any sound, but I am processing imput, which appears to have little overhead.
You should see much better results than 24 fps.

Dan East
a backbuffer->frontbuffer copy should take you around 10ms if you make a special case copy for the ipaq's (rotate screen doing a copy) , in case you use a generic approach you will need around 15ms. A simple memcpy does it in 5-6ms (if you keep your backbuffer rotated just like ipaq screen)

24fps is the refresh rate of ipaq's screen therefore to get smooth animations your game should run with more then 24fps.
I was afraid this would happen - my mistake was really stupid.  I had a timer proc that called my GRender() function, but for whatever reason, I had set it to call every 20 milliseconds.  I set it to zero, and suddenly I'm seeing 36-40 fps.  I hate mistakes like that.  In the long run, I did get my graphics code heavily optimized, so I guess not all is lost.  Thanks for all the help!
Move everything you can out of those nested loops.  You can clip the srcRect to the screen before you get into any loop.  Also don't do the alpha test each pixel if the 'alpha' param is false.  The alpha (colorkey) test should be one of the first things you do before you get inside any loop.  Implement the blitter code twice (once with alpha test, once without) and test the alpha flag ahead of time.  This allows the non-alpha blit code to only contain a single loop for the height of the blit and then you can use memcpy to fill the scanline.

Those things alone should get you a handsome speed increase.  You can take it further by reading and writing DWORDs instead of WORDs but you have to worry about alignment issues.  Beyond that, the ARM has an instruction where you can read/write to multiple registers in one go. It makes things like memcpy operations very fast.

24 FPS is OK if that's the frame rate of your finished game.  However it sounds like your blits alone are causing you to not run faster than that.  That's bad and you can do much better.  The numbers Mirek quoted for memcpy would be over 200 FPS, and you can do better than memcpy if you're clever.

Digby:Could you possible show us how to do it better then memcpy? It would be surelly a great addon to Jacco's EasyCE lib.
Using the multiple register load / store is faster than memcpy can possibly be. Memcpy itself can't use that approach since it doesn't know in advance how large the area is that needs to be copied. For EasyCE, you could use 12 registers to load 24 pixels at once, and save them with the next instruction. If the cache doesn't spoil the fun, this should be seriously fast, especially when you unroll the loop a bit... It's simple asm code too. :)
yeah, but a person without a PPC with strongarm processor can just sit there and watch you talking about simple asm... asm isn't simple if you can't test it:)
about memcpy:
well if they would make it right there would be something like this:
while(>24bytes to copy)
{
 use this funky copy with all registers
}
process other bytes with dword/byte copy

I guess it would be much faster and even if you copied under 24bytes one if wouldn't make a very big difference
A few things about memcpy: It doesn't know if you will or won't be passing in a DWORD aligned source or destination buffer each time, so it has to test this each call to know if it can do DWORD read/writes.  You will know this already and should only have to test for it once.  It also has function call overhead which you won't have if you implement the copying inside the inner loop of your blit routine.

That being said, there is some goodness with using memcpy.  It's already written and bug-free. It handles unaligned data.  It IS pretty fast.  It runs on all platforms.  And the most important - it probably isn't the bottleneck in the average game.

What I've done is implemented my large, DWORD aligned blits using the multiple load/store register instructions on the ARM.  On other processors I just use an optimized loop written in C.  These are used for my 'flip' routine which is the back buffer to front buffer full-screen copy. It's also used for blits that are larger than a certain threshold.  All of my other blits are written in C.  I do have separate implementations for colorkeyed, alpha, and ROP blits so that there aren't tests in the inner loop if there doesn't need to be.

To overcome the function call overhead (which isn't much on the ARM) I've written the entire flip routine in assembly.  If I get time this week, I'll post some benchmarks if you guys are interested.


I haven;t got a PPC yet. Althrought I plan to get one sooner or later (lack of cash atm:() so I'm very interested in any details about strongarm and overall PPC optimizations. It would be really welcomed if you post something like this.
Thanks to all the great reccomendations, I rewrote my key blitting functions, and am now obtaining ~72 fps with a fullscreen blit, oriented to right-hand landscape mode.  My concern is that because of the way that I am traversing my back buffer in left-hand mode, (putting my pointer to the end of the buffer and marching backwards) I can't implement the scanline memcpy like I could with the right-hand mode.  Is there any way to avoid this?  even if I kept the backbuffer orientation the same, won't I spend the saved processor cycles "rotating" the back buffer when I copy it to video memory?  Being left-handed, left-hand landscape is somewhat important :)
There's another reason to blit to the display from the top-down, as the hardware is oriented. If you are planning on doing smooth, whole-screen scrolling, then if you do not blit in alignment with the display, you will see visual distortion where the scanlines are being drawn out to the actual display, and you are updating them from another direction.

Dan East