Sunday, November 18, 2007

ANIM_hide is not a framerate optimization

I'm sure I blogged this before, but the blog's gotten old enough that I can't find my old posts. This came up in an internal company discussion, so let me say it again:

ANIM_hide is not a framerate optimization. 99% of the time it doesn't make your object draw faster.

ANIM_hide is provided to let authors make certain animated effects (like swapping out a prop disc for blades based on prop RPM). These are animated effects and cost fps, because they use the CPU and interrupt the graphics cards from just blasting out triangles at warp speed. An example:

You might have a very complex object attached to a plane that doesn't need to be drawn at all, like a landing gear assembly. This object could have its own texture, a ton of animations, and high triangle count. Using a giant ANIM_hide around it to prevent drawing when the gear is retracted does you no good. Here's why it doesn't help.
  • Before an object can be drawn, X-Plane prepares the geometry and texture to be used by the card. These are atomic operations (we have to prepare the whole texture and all of the geometry no matter what we will actually use) and they are expensive.*
  • X-Plane then must evaluate the animation command on the CPU to decide if the object must be drawn. If the object drawing is simple and the object is drawn, this CPU work is just wasted, so this technique wouldn't even make sense unless the object is hidden a lot of the time.
  • If the object is hidden, X-Plane will still run through every command in the object, simply skipping drawing. So if the object is heavily animated, we still pay a lot of CPU for the "hidden" object.
You will actually save some performance with ATTR_hide in one situation: if the geometry that is not drawn with ATTR_hide covers a huge amount of the screen (e.g you are really close to the object) then not drawing the pixels saves frame-rate. This would be extremely unlikely - you'd have to hide an entire cockpit from the inside to see any kind of benefit with this.

Why doesn't ANIM_hide actually skip work? Well, it may someday, but for now it's a question of how we optimize objects. When we load objects, we evaluate their command sequence and attempt to consolidate and remove unnecessary CPU work. We remove unnecessary state change, combine drawing commands when possible, etc. This works because we know at object-load time exactly how the object will be drawn. But consider this bit of OBJ "code":

ATTR_flat
ANIM_begin
ANIM_hide
ATTR_smooth
TRIS 0 30
ANIM_end
TRIS 30 60
ATTR_smooth
TRIS 90 120

If ANIM_hide really skipped work, is the ATTR_smooth routine necessary? Can the last two TRIS commands be combined into one big TRIS command? If ANIM_hide removes an attribute, we don't know until we actually draw the object what attributes will be run. But if we say ANIM_hide doesn't affect state change (which requires us to actually do the state change) we can then realize that none of the state change in this object are actually necessary, saving 3 attributes, and allowing us to combine a triangle batch.

So for now our policy is one of simplicity:
  • ANIM_hide is for artistic purposes, but not optimization purposes. It simply stops drawing, it doesn't eliminate work.
  • The optimizer then goes to work on your object.
  • I think we will someday provide a new mechanism for the "landing gear case", one that's specifically designed to quickly eliminate parts of a plane that are expensive and don't need to be drawn.
* X-Plane does try to optimize out this cost in some cases. For the purpose of drawing, an object can get "culled" (not drawn at all) by being too far away for LOD or fully off-screen, or it can get "drawn" (meaning we do look inside the commands). Culling is very very fast, and drawing is usually quite slow. The optimization of avoiding using geometry and textures always work in the culling case, but usually do not work in the drawing case. So having a smart LOD on the landing gear is a huge win.

No comments: