XNA 4.0 presents Vertex and Pixel Shaders 3.0, which support Hardware Instancing.
Hardware Instancing works like this: Say you have an object (e.g. a projectile of some sort) that you want to draw a whole bunch of times (a barrage of said projectiles). It's really CPU intensive to make a bunch of draw() calls with separate transformation matrices. Instead, one can load all the transformations into a separate vertex buffer and let the GPU sort it out with a single call to draw() (which it will -- it will automatically duplicate the mesh vertices for each transformation matrix).
Sounds great, right? Definitely. Instancing is a critical technique for exactly the kind of situation I described (a lot of projectiles flying about). Unfortunately, there's a catch. A slight difference in behavior between running on Windows and on Xbox. Let me esplain. No--there is too much. Let me sum up:
A Model (the logical representation of one complete object) can be made up of multiple Meshes (the logical representation of all vertices that make a single solid part of said object). In Windows, instancing an entire Model works perfectly. On Xbox, however, what makes it to the screen is one complete model (the first instance), and then a bunch of copies of the first mesh for the rest of the instances. Say your Model is a rocket, made up of Meshes for the body and individual tail fins. Say that a tail fin happens to be the first Mesh in the Model. Now imagine that you're trying to draw a bunch of rockets flying around, but what you get is one whole rocket and a bunch of disembodied tail fins flying around.
One workaround is to combine all the vertices in the entire Model into a single Mesh. This is bad for a number of reasons. First of all, for me, it means hand-editing the .fbx file that stores the Model data. It's possible; the file is human-readable, but it's a really awful task. The second reason it's bad is that it screws up texture mapping. Each vertex in a Mesh has a set of texture coordinates, that can refer to exactly one set of textures for the entire mesh. This is exactly why Models often have more than one Mesh, so that different sets of textures can be used for each part of the Model. So for a single-Mesh Model, you'd have to squeeze all your textures into a single image and re-map the entire model. Possible, but hugely impractical for my operation.
Anyway, this leads to much head-to-wall banging for me as I try to work around the problem. I've posted a thread in the XNA community forum for assistance; hopefully one of the really smart folks will jump in and help me out.
No comments:
Post a Comment