Optimizing texture animations

Started by
5 comments, last by Enokh 20 years, 6 months ago
I'm working on a 2D top-down spaceshooter game/engine and I've come to the point where I'm working on flat/2D animations by placing a quad and texturing it in a certin order to create an animation. Here's the basic way to do it as I see it: Create 5 identical images, each image as a frame for your animation. Then in your code load them all up into RAM at the start of your game, bind the first texture onto the quad and then every fifth of a second (or any other number) re-bind the next texture in correct order onto the same quad. Optimization one, which I got off this forum: Create one image, with the 5 frames side by side. Load up that one image into memory and then simply play with the texture coordinates so that every fifth of a second they are moved over EXACTLY to the next frame in line. Same RAM usage, but less CPU cycles used as you don't have to rebind the texture nearly as much, the only thing you're doing is playing around with the value of a float, which is functioned in the texture coordinate paramters for the quad. Optimization two, which I just thought of while taking a dump (it honestly seems that you get the best ideas while on the crapper): Create 5 images for the 5 frames, and instead of loading all 5 images at the start of the app, only load one at a time as the client needs to view them. You only have one texture object for all 5 images - this means that at the animation loop, every fifth of a second you load the next image in line into your single texture object, and never re-bind as the texture object itself changes from frame to frame. One fith of RAM usage (or one / Number_of_frames_in_animation) at any given time, but more CPU cycles eaten as you have to execute the load-texture-into-memory code every frame. Question is, which optimization is better? PS: I work with TGAs, and I got the code to load TGAs off gametutorials.com, which looks pretty standard to me. [edited by - Enokh on October 11, 2003 1:21:24 PM]
Advertisement
What hardware platform is this for? It seems un-clear what you mean by "RAM" or "memory" -- in all cases, the same amount of system RAM is used; the only difference is the amount of uploaded texture RAM in use (i e, on-card VRAM).

The amount of "bound" texture RAM only matters if you actually run out. If you assume that all graphics cards have 32 MB these days, and 12 MB goes to frame buffers, then you have 20 MB to play in. That''s a LOT of images for a 2D shooter!

Thus, I''d absolutely do it using the 5 images in one texture way.

However, when you say that "cpu cycles" are used to upload a texture image, that''s rather inaccurate. Most cards these days can upload textures asynchronously. In fact, they draw everything asynchronously, and may run more than a frame behind in rendering compared to what you''re issuing! The problem with texture uploads is memory bandwidth and bus bandwidth. Your drawing commands and texture uploads share the AGP bus, so if you upload a lot of textures, your vertex throughput will be lower. If you''re a 2D shooter, vertex throughput is unlikely to be limiting you. However, uploading textures also take RAM bandwidth from the main memory that the CPU is also executing out of. If you run some horribly complicated simulation for your game, or upload a large amount of texture data each frame, then this will be a problem, and keeping everything in VRAM is a good idea.

If you''re targeting lame hardware (TNT2, ATI Rage, Intel i810, Voodoo 3, etc) then some of this has to be modified to take software transform into account, but it''s still largely useful advice.
enum Bool { True, False, FileNotFound };
Unless you have huge amounts of animation frames(something that doesn''t even fit into VRAM) go for 1st "optimization".

I have 3rd option for you. Load all animation frames into single 3D texture and use r coordinate for animation (looping). This will also give you smooth transitions from one frame to another for free (-small penalty for 3D texture usage).

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
"What hardware platform is this for?"

Sorry, my game is intended for Windows, and basically as lower on the card/PC scale as possible. I'd love for my game to run on a Voodoo =)


"Load all animation frames into single 3D texture and use r coordinate for animation (looping). "

Never used 3D textures before, could you expand a little more about them and the r coordinate, or point me to the right direction?


"and keeping everything in VRAM is a good idea..."
Certainly, but the memory usage is growing in an alarming rate. In-fact by the time I get all my art done I doubt that'll even be the case even any more (All textures on VRAM, that is) with my 128MB GeForce4, this is because I'm using 32bit TGAs for the alpha channel capability, every image is anywhere from 128KB for my ships, to a few megabytes for my backgrounds! I have one laser animation that's 14 frames long (the image is around 4000~ pixels wide), and that image is weighing in at a hefty 3 MBs. What do you suggest I do on that front? I mean many games use hundreds of textures, how do they pull it off without HD swapping?

[edited by - Enokh on October 11, 2003 1:56:21 PM]

[edited by - Enokh on October 11, 2003 6:55:17 PM]
"Create 5 images for the 5 frames, and instead of loading all 5 images at the start of the app, only load one at a time as the client needs to view them. "

That is a really bad idea, the loading will slow down your game by a very large amount.

"Create one image, with the 5 frames side by side."
That is what I do, it works well. You can also use a 3d texture as well.

It sounds to me like your largest concern is that your textures are taking up too much memmory?
If you have a texture that is 4000 pixles long per frame, you have a problem with how your utalizing textures. Most lazer effects can be done with a 1d texture and some creativity.

It sounds to me the problem is not your optimizing but your use of textures.
Nah it''s 4000 pixels wide for the entire 14 frames (or 4096, to be exact). It''s basically a cylinder with a glow effect that increases in intensity until it''s as bright as a light bulb, then I play them in reverse order so it pulses around like lasers tend to do in video games. Actually in my animation code I can tell it which of 3 animation types to use, it''s pretty nice. Type 1 to play the animation once, type 2 to loop (0 to 5, 0 to 5, and so on) and type 3 for a rythmic loop (0 to 5, 5 to 0, etc). Just blabbing about --

Could anyone give me a link to a decent explanation/tutorial for anything but 2d textures? I have no idea what is a 1d or 3d texture, would appreciate any help =)
Try this sight out for all of your questions about 3D textures
and for anything else you can think of...
Developer.nvidia

A 1D texture... think about it. A 2d texture has a width and
height where as a 1d texture is just a single line of pixels.
"Make it a habit to be loyal to the activities that serve the highest part of yourself."

This topic is closed to new replies.

Advertisement