PocketMatrix forums

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

Why dont i get 2.5 times FPS SpeedUP? I realy should.

I have removed all that math stuff cose nobody wants to read it.

So the question:

Is there a more faster way to do

Code:

display->Blit(pos.left,pos.top,m_background,&Rect;(pos.left,pos.top,pos.right,pos.bottom));


for example direct copying bitmap bits or memory segment that belonges to the bitmap, streight to the screen.This is becouse i dont want all image to be displayed, just area that intersects with a sprite. See example below it gives about +2.5 fps to the sample Blit project on my Toshiba e740 when it should give about 2.5*FPS

Code:


void BlitSample::GameLoop()
{
DisplayDevice* display = GetDisplay();

#if defined(FROG_HPC)
display->Blit( 320, 0, m_background );
#endif

for (int i = 0; i < 4; ++i)
{
Rect& pos = m_position[i];
display->Blit( pos.left, pos.top, m_images[i] );
}


// Update FPS
m_FPSTicks[ m_FPSCounter & 15 ] = PocketPC::GetTickCount();
if (m_FPSCounter > 15)
{
uint32_t totalTime = m_FPSTicks[ m_FPSCounter & 15 ] - m_FPSTicks[ (m_FPSCounter+1) & 15 ];
if (totalTime == 0) totalTime = 1;
uint32_t fps = 16000 / totalTime;
uint32_t n100 = fps / 100; fps -= n100 * 100;
uint32_t n10 = fps / 10; fps -= n10 * 10;
uint32_t n1 = fps;

if (n100 > 9) { n100 = 9; n10 = 9; n1 = 9; }

// Display fps
display->Blit( 0, 0, m_numbers, &Rect;( n100 * 8, 0, (n100+1)*8, 11 ) );
display->Blit( 8, 0, m_numbers, &Rect;( n10 * 8, 0, (n10+1)*8, 11 ) );
display->Blit( 16, 0, m_numbers, &Rect;( n1 * 8, 0, (n1+1)*8, 11 ) );
}


display->Update();

for (i = 0; i < 4; ++i)
{
Rect& pos = m_position[i];

display->Blit(pos.left,pos.top,m_background,&Rect;(pos.left,pos.top,pos.right,pos.bottom));
if (i != m_dragImage)
{

switch (rand() % 3)
{
case 0: break; // Don't move
case 1: if (pos.left > 0) pos.Translate( -1, 0 ); break;
case 2: if (pos.right < display->GetWidth()) pos.Translate( 1, 0 ); break;
}

// Move image (vertical)
switch (rand() % 3)
{
case 0: break; // Don't move
case 1: if (pos.top > 0) pos.Translate( 0, -1 ); break;
case 2: if (pos.bottom < display->GetHeight()) pos.Translate( 0, 1 ); break;
}
}
}
++m_FPSCounter;
}

Well, I don't know how you are counting. I was about to write a reply on this note before, but I didn't have the time.. And now that you've removed the part about you getting 2.5 times higher FPS it just doesn't make any sence. (but you counted wrong, this much I'm shure of).

Anyways.. Here's why and how you might do.

The Blit is, in fact, a direct copy of bitmapped data to the secondary image buffer. There is no "faster" way of doing this copy. The bottle-neck is when you call display->Update(), because it'll write all the data of the secondary buffer (or backbuffer as it's called) to the screen.

You can remedy this in two possible ways (and perhaps even more, since I really don't know what I'm talking about here. Just guessing =)

1. You try and see if you can get your hands on the frontbuffer. It's not available on all systems though. If it is, you can change the rendering target to this buffer and render directly to the screen.

2. Supply the Update() with an update rectangle. This will enable you to only blit the area affected. I do however fear, that the update rectangle hasn't been fully implemented. You just have to try and see(because I don't have time to read through all the code.)

And finally, a comment on the code:

One usually blits the background first, and update the screen last in an animation function (or graphics function). Not the other way around. This will only confuse you as your program get's larger. And you don't gain anything from rearanging the code the way you've done as far as performance goes. Just thought you should know.
Versa_CHI · 09 Feb 2003, 20:00 UTC — Here is the math
Hi Adde.

Its quite interesting thing you told about Update rectangle but again, as allso you have mentioned that not all systems will allow me to do that, and it's kind a hard to implementate such system programing code for me (at my current level). Even for some systems which will allow me to change the front buffer. So i think it's better to let that idea to fly for a while (while somebody not implementates it)

And a last thing about drawing after display->Update();

It's not drawing - it's hiding. I've made it by the sheme

1.Draw sprites;
2.Update screen;
3.Hide sprites;
4.Move sprites;

Oufcourse it could be made like that:

1.Hide sprites;
2.Move sprites;
3.Draw sprites;
4.Update screen;

But it seemed for me that its kind a paralogical to hide a sprite before i drew it and i think that there's nothing confusing about that.

So here's the math

Math:

Background Image = 320*240*3 = 230400 Bytes (Background image is 24bit image 24bit=3bytes)

(sprite images are 256 color images and thats 1 byte);

Chicken Image = 80*80*1 = 6400 Bytes
Drum Image = 80*80*1 = 6400 Bytes
Trumpet Image = 80*80*1 = 6400 Bytes
Telephon Image = 80*80*1 = 6400 Bytes

In blit sample we draw 6400*4+230400 = 256000 bytes per loop
and get 48 fps

Changed sample works by sheme:
before game loop start's it draws background;

in game loop

1.Draw 4 sprites
2.Update screen;
3.Hide sprites with 3 bytes/pixel background image (blocks with size of sprites)
4.Move the sprites

so there we have

6400*4*3 + 6400*4 = 102400 bytes;

and this is the key for 2.5*FPS becouse 256000 = 2.5*102400 and i think that all the algorithms like blit are at least linear and will double the fps.

I dont know for sure perhaps blit allways translates images to 16 bit color and only than works with it, so it may be the one mistake in math and the above sheme wont work, i dont know for sure.
But it seemed for me that its kind a paralogical to hide a sprite before i drew it and i think that there's nothing confusing about that.


I'm just saying that this is the customary way to do it. If you don't like it, fine. There are no right and wrong in coding as long as you get the same result.

To me though, it seems like there is no point in doing work you won't benefit from until the next time WindowUpdate() is called. If you think of what that functions purpose is, it's to update the window (one time). Ok, fine. Since we call it several times, it doesn't matter how we partition the code. It'll all be a loop or a circle anyways. You can even do the update first if that makes you happy.

Code makes much more sence (by yourself in a couple of months and others) if they have a clear logical structure. For instace, a lot of time should be put on naming functions and variables so they make sence.

But nevermind that. As long as it's working, it's ok. Just giving a few pointers.

So, back to the math.. Since PF uses a 5-6-5 palette (ie: 16 bits), I assume that all the images and blits are treated the same and so we can just focus on the pixels.

Original Blit demo:
Background blit: 320x240 = 76800
4 Image blits: 4x80x80 = 25600
-------
102400

Modified blit:
8 Image blits: 8x80x80 = 51200

Ok.. so there should be a x2 increase in FPS you might say.

But, this is just the blits to the backbuffer. Each time display->Update() is called, the whole backbuffer is copied (or blitted if you will) to the screen, which means an additional 76800 to each version, resulting in a 0.71 ratio or a possible (theoretical) increase in FPS by 1.4 (or 19 FPS).

Ok.. I agree that it's strange that it only increases 2.5 FPS, but you got to keep in mind the maximum memory bandwidth of your system and the maximum screen update frequency. Perhaps your system has a maximum of approx. 50 FPS, and in that case, no matter how fast you made the blit samle (or how much code you remove) you will only get a 2-2.5 FPS increase (from 48 that is =)

There are several ways you can test this. I give you two:
1. The maximum framerate is easily calculated by just blitting the FPS counter on an empty background (simply clear it with display->Clear(Color(0,0,0)), or make shure that you always display the same amount of digits)

2. Compile your code for PC using VC6 and test it. You should get a clearer result.

There could also be other explanations, like HOW the backbuffer is copied to the screen. I'm not an expert on GAPI, so I can't speculate on this. But it seems locical that a blit (of a 320x240 image) to the backbuffer is a tad faster than an update of the backbuffer to the screen, because you need to lock the screen (or so I think, but I'm not shure) and/or other precautions. I'm pretty shure that Thierry (the author of PocketFrog) has implemented the Blits as fast a possible though.

Anyways. The bottom line is that you can't calculate the way you did. Not even the way I did and think that it applies to FPS as if it was a linear function.

If someone with some real knowledge about GAPI and PocketFrog could confirm or discard my explenation, I'd be glad. Because, and I've said it before, I'm just guessing here. Although the guessing is based on some facs and experiences.
until the next time WindowUpdate() is called


I'm sorry. I've written my own extention to the PF library and I renamed the window callbacks to WindowInit(), WindowEnd() and WindowUpdate() instead of GameInit(), GameEnd() and GameLoop()..

So what I ment was GameLoop() and then (by the name) it's not that clear what it's suppose to do (at least not by my definition). Or, you might even say that it's suppose to loop the game, and then your code does make sence.

Sorry bout that =)

Once again, it's important how you name your functions and variables.
... but you got to keep in mind the maximum memory bandwidth of your system and the maximum screen update frequency.


For those who don't believe a word I just said, take a look at the following thread where Thierry basicly says the same thing:

http://forums.pocketmatrix.com/viewtopic.php?t=7694
Versa_CHI · 10 Feb 2003, 22:50 UTC — Math
Hi adde

So, back to the math.. Since PF uses a 5-6-5 palette (ie: 16 bits), I assume that all the images and blits are treated the same and so we can just focus on the pixels.

Original Blit demo:
Background blit: 320x240 = 76800
4 Image blits: 4x80x80 = 25600
-------
102400

Modified blit:
8 Image blits: 8x80x80 = 51200


Arent we forgetting something ? I agree that screen size is 320x240 but for each represented pixel on that screen, 2 bytes of memory is reserved and that actualy doubles the delay. Thats why while not forgeting to sum to results the Update() function (ether my mistake i havent mentioned it) allso those results should be doubled becouse when transferring pixel from memory to back buffer we actualy transfear not one byte but two
though next results are more accurate:

Original Blit demo:

Background blit: 320x240*2 = 153600
4 Image blits: 4x80x80*2 = 51200
-------
204800+(Update screen)320*240*2 = 358400 bytes

Modified blit:
8 Image blits: 8x80x80*2+(Update screen)320*240*2 = 256000

though: 1.4 (19,2 FPS)

:) how accurate are you adde

But still talking about maximum screen refresh frequency, i can't agree with you. I dont think that display refresh rate can change the program's fps becouse it's device independent (i hope so becouse only old computers had done everything for display device and modern computers only transfear memory to a specific memory block and display device which has its own cpu copys it to hardware, its much more more... beautiful or structurized)
Though theres no any 50 fps limit. I've switched off even display->Clear(Color(0,0,0)) so its just fps counting routine and display->Update() left and it gave around 110-100 fps.
As for memory bandwidth issue:
lets just think logicaly if i decrease memory use by 1.4 times per loop then what should i get then (in ideal)? Oufcourse 1.4 times more loops for the same time. Why? I can answer this question. Becouse memory transfear algorythms are O(kX) (that's linear-time) class of algorythms where k is coeficient of linearity(dont know if thats the right translation) which approx = 1 at our sample and X is basicly a number of bytes though what should be a problem then?
I think that this 1.2 - 1.4(20%-40% how large isn't it) coeficent is a system takeover time. Becouse Pocket Pc should take time to do something it thinks important. So i think that this is where performance disapearing.

PS.

Cant remember for sure but i think i've seen somewhere those system extention utilitys (for development) or those were librarys cant say for sure but the point is that thay gave a system-class priority or something like that.
Glad we could agree on one thing though.. That the maximum theoretical increase in FPS is 19.2 (whether or not you count bits, bytes or pixels doesn't really matter since it's the ratio we're after).

I'm out of ideas why you only get 2.5, having a maximum FPS count of 110. Perhapps you could find the answer if you dig into the code of DisplayDevice::Update(). That's where the bottleneck is anyways. For instance, if you do it the other way around. Add stuff to the loop and expect a large drop and notice it didn't drop as much. I wouldn't be surprised.

Sorry I can't help you more. I don't have detailed information about GAPI (or DirectX for that matter) and display devices. The closest I've got is coding ASM (assembler) on INTEL x86 computers. And then you had to wait for the video rasterizer to finish updating the screen before you could write to video memory again, or you would get funny results (like flickering, and updating only half or first part of the screen).

Hope you figure it out. If you do, post a note here to let me know.
We've discussed this elsewhere before, but just in case it helps, the StrongARM based hardware (usually) works like this:

There is no specific buffer to hold video memory (i.e. no VRAM). The buffer that's displayed is sitting somewhere in the SDRAM.

Usually the StrongARM's internal LCD controller (and corresponding DMA) is used to transfer the contents of the "screen" from the buffer to the LCD in chunks of 16 bytes (IIRC).

The LCD's DMA is the highest priority device in the system - i.e. if the LCD is screaming for data then even the CPU is blocked out until it's serviced.

Bear in mind that normally the buffer(s) that are used for the LCD are unbuffered and uncached. They're slow to access and use. A blit to screen is really just a memcpy() to the uncached buffer that the DMA is using from whatever (usually cached) buffer that you plotted into.

The update of the LCD is limited to the refresh rate of the LCD in the device (IIRC for Ipaq's it's 70Hz).
There's no easy way of getting a vsync hooked up, so 70Hz isn't a hard limit - it's just pointless going above it.

HTH,

Ref.
You know, I decided long ago to hard-code all my work to 30FPS. That way I can pretty much guarantee it will maintain a steady frame rate on any device (I use my stock E-125 as a baseline... if it can churn out 30FPS in whatever I'm doing consistently, I know I'm good on any modern device).

So, let me pull a Bill Gates here...

"30FPS should be enough for anybody!"

And if your still not laughing, try coding on a C64 where your trying to update a color table in between HORIZONTAL SCAN RETURNS (approximately 1/3 of 1/30 of a screen refresh, give or take, I forget... It WAS 15 years ago!)
Thanks Refractor for clearing this up. I figured it was something like that, but I wasn't sure.

And I have to agree with fzammetti too. 30 FPS should be enough for everyone.

Personal remark: Coding for C64. Respect! I only played games on my C64 (and I still have a working C64 in a box back at my parents house. One of the joysticks has stopped working but I hope I can fix it during the summer. Who needs Playstation II when you got Krackout, 1942 and the Last Ninja on C64?)
Thanks! I've recently picked up a X1541 cable, so I've been transferring all my old discs to PC to run in an emulator. It's a total walk down memory lane when I find a demo or intro I did all those years ago. Plus, I don't care what anyone says, but games like Hardball, One On One, Impossible Mission, Space Taxi (nobody get any ideas, *I'M* doing a PPC remake of that!), Spelunker, Hover Bover, Barbarian, Satan's Hollow, Serpentine, Beach Head (not this remake crap we have today), Raid Over Moscow and GI Joe just have no equals today.