Caching graphics to avoid drawing delay?

BlitzMax Forums/BlitzMax Programming/Caching graphics to avoid drawing delay?

Grey Alien(Posted 2006) [#1]
OK, I'm gonna ramble a bit here but perhaps some of you knowledgeable people can help me ... :-) please

On my zooming in map screen, I noticed that when it first appears and begins to zoom there is a jerk during the zoom in anim. This is caused because the first frame takes longer to draw than the rest and the delta time code "catches" up and jumps ahead a bit when drawing the second frame. Then all other frames are fine.

So why is there a delay drawing the first frame?

I found a similar thing in BlitzPlus and it's because the image is held in RAM and the first time it's drawn it get's "uploaded" to the video card VRAM before it's drawn. Then each subsequent time, providing that the image hasn't been edited, it just draws straight from VRAM (no upload required) so it's faster.

My understanding of BMax was that it didn't need to do this (unless you are using pixmaps) because all the images are in VRAM anyway.

However, I made a change to my code where I draw the first frame but don't call flip (so it can't be seen) and then I do it properly with flip and this seemed to resolve the jerk problem. This infers that there is some kind of caching/optimisation going on when the images are first drawn. Is that the video card or BMax? Or am I deluding myself and I'm seeing the jerk (and disappearace of it after a dummy frame is drawn) due to some other issue with my code.

Anyone know anything about this and the possible reasons why it may occur? I'd really like to have some standard code in place that optimises the graphics for each screen, or a game level, so that's it's all ready to draw at top speed the first time it's used without any delay, that would be cool.

Thanks in advance for any advice, knowledge, speculation etc ;-)


BlackSp1der(Posted 2006) [#2]

I found a similar thing in BlitzPlus and it's because the image is held in RAM and the first time it's drawn it get's "uploaded" to the video card VRAM before it's drawn. Then each subsequent time, providing that the image hasn't been edited, it just draws straight from VRAM (no upload required) so it's faster.

My understanding of BMax was that it didn't need to do this (unless you are using pixmaps) because all the images are in VRAM anyway.


Images in Blitzmax are pixmaps too, internaly. the first time it's drawn an imageframe is created from the pixmap.
I use a function to build the imageframe after load the image.
Function ActiveImage(image:TImage)
	For Local _frame:Int=0 Until image.Frames.Length
		image.Seqs[_frame]=GraphicsSeq
		image.Frames[_frame]=_Max2DDriver.CreateFrameFromPixmap(image.Pixmaps[_frame],image.Flags)
	Next
EndFunction



Grey Alien(Posted 2006) [#3]
BlackSp1der: Aha, cool so you've verified my theory. thanks for the code snippet. So that effectively caches them on all the video card, cool.


Robert Cummings(Posted 2006) [#4]
Just do a warm up cycle, most commercial games do.


Grey Alien(Posted 2006) [#5]
Just do a warm up cycle
You mean draw everything without call flip or do as BlackSp1der suggests? Actually I was wondering if you don't call Flip if BMax/DirectX/GFX card actually bothers to transfer the image to VRAM in such a case or if it is "optimised/intelligent" and only does the transfer when Flip is called?


BlackSp1der(Posted 2006) [#6]
drawing without flip or calling the function have the same effect. The imageframe is builded (uploaded to VRAM)


Grey Alien(Posted 2006) [#7]
OK, thanks that's worth knowing. So I could call this at the start of the game when everything is loaded. But what about this:

- I load in some graphics for the first time on level 1
- I call the imageframe code.
- At the end of the level I set the images to null for Garbage Collection (does this clear them out of VRAM too? probably not...)
- For level two I load in some of the same graphics (not all the same).
- I call the imageframe code again.
- What happens to the old image which was the same, does it remain in VRAM cluttering/taking up space, or will it get shuffled/celared out by the gfx card eventually?
- Also what if I don't free up the image and I just call the code to build the image frame twice, will that cause a problem, or two of the images in VRAM, or is it intelligent and only has one copy? Any ideas?

Thanks.