Slow first DrawImage

BlitzPlus Forums/BlitzPlus Programming/Slow first DrawImage

Grey Alien(Posted 2005) [#1]
OK I have a game that loads in graphics into templates and then makes objects with graphics that point to the templates. Also sometimes I use CreateImage with the all important 1 flag on the end! I time all the game routines in millisecs and output them for debugging/optimisation.

I have noticed that on some video cards the first time a graphic is drawn (with DrawImage or DrawBlock) there is a spike in the amount of millisecs taken to draw it e.g. normally takes 1 ms but suddenly takes 10ms. Then the second time the graphic is drawn, all is fine. This happens with tiles, enemies, pickups, fonts, all different types etc but not everything, to be honest it is a bit inconsistant and is not noticeable on some machines. I have tested it with sound off to make sure that was not the problem.

So I am asking how are loaded and created images stored in Blitz? I know they are "managed" based on the flags but what manages them, Blitz or the video card? Is there some kind of video caching going on? The game uses hardly any resources and I have run it on good machines so I doubt very much it is using virtual memory and my code loads everything before the level is displayed.

Any ideas? I know this is quite a techincal point but I was hoping someone knew. I realise it could be some kind of bug on my part, but before going on a wild goose chase I was hoping someone else knew for sure ...

Many thanks in advance.


Rck(Posted 2005) [#2]
I have had to deal with this on my most recent project. I made my own kind of mem management with freeing and loading tiles from disk for a game I'm working on.

From what I've seen and guessed,
Managed images go into program memory but do not take up actual video mem until they are used by DrawImage, etc. Also, soon after, the images are removed from video mem if where they were drawn to a buffer which is then freed. Another case to handle is when you have a lot of images loaded as managed. If their size (say 1024 x 768 x 4bytes x number of the screen sized images) bytes is too much for your card, a slower memory is used and CPU will go up.

In summary, its not just enough to LoadImage managed, actually drawing them to a 1,1 image's imagebuffer, or to the BackBuffer, etc. will "push" them into vid mem. From then on, things should run smooth.


Grey Alien(Posted 2005) [#3]
Aha, so basically I should draw all my graphics to a hidden buffer when I init the level and then it should run faster, hopefully, unless the video card is really pap. Thanks Rck, I was beginning to think I was going mad.


Grisu(Posted 2005) [#4]
It might also be an issue how you organised your freeing of unused stuff (types, images etc).

But without having a look at the code, hard to tell.


Grey Alien(Posted 2005) [#5]
I have been very careful to free everyhting correctly and have done tests to see if any resources are missing after the game has been run. Also the slow drawing time only occurs the first time you play the game which is before anything is freed. But thanx anyway.


Rck(Posted 2005) [#6]
Use some code like this and run it in the background to see how the mem is loaded only once images are drawn after loadimage




Grey Alien(Posted 2005) [#7]
thanks rck, I'll give it a shot soon.