Jump to content
  • Advertisement
Sign in to follow this  

Geometry Instancing Examples/Tutorials

This topic is 4687 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I know this question has been asked before but I am still at a loss. I am after a really basic example of some geometry instancing. Preferably not hardware instancing as I don't have a machine with a card that supports it available to me here. The one in the Directx SDK is cluttered and i do not quite understand it. Thanks for your time, Alex

Share this post

Link to post
Share on other sites
The bottom line is, instancing, especially when you aren't using hardware, is just pretty hard to manage. You have to have everything in your engine working accordingly, otherwise you will run into fundamental design flaws. Here is how I do it:

Load Time:
Load all of your meshes from file. Use some class to determine the maximum number of each mesh that may be drawn at the same time. Remember, 2 nodes can only be instanced together if they:

(1) Use the same Shader
(2) Use the same Technique
(3) Use the same Constants (this includes textures)

For all intensive purposes, you can just group in:

(4) Uses the same geometry

since meshes that use the same constants are almost always going to be the same mesh.

Anyways, once you figure out how many of each instance you have (say this number is n), you need to make n copies of its vertex and index data. It is important that your vertex structure contains some way to identify what instance number the vertex belongs to. For example:

struct Vertex
float InstanceID;
D3DXVECTOR3 Position;

This InstanceID is important because we will use it to access arrays of instance-dependent data in the shader. For example, each instance is going to have a different world transform matrix. To accomodate that, we will have an array of world matrices in the shader, then simply index that array based on the vertex's InstanceID.

When copying the vertex data, make sure you adjust the InstanceID accordingly. Also, when copying the index data, make sure you adjust the indices (ie add numVertices * currentInstanceNum to each index).

Run Time:
At run time, we must do the following:

- For each mesh that is to be rendered, assign it one of the instances available
- Set the mesh-dependent constants in the shader. For example, with the mesh's world matrix, we want to set it at the ith element in the shader, where i is the instance number you are on.

You may encounter an additional challenge if your buffers aren't big enough to fit in *all* of the instance data. For example, lets say you need to render 5 meshes...however, your buffers are only big enough to fit 3 copies in. In that case, we would just render the entire buffer once (3/5 instances), and then repeat the process, but only for the 2 remaining instances.

I hope this clears some things up for you. Software instancing is a pretty complicated process if you don't know exactly how to go about it.

Share this post

Link to post
Share on other sites
Original post by Aiursrage2k
Theres a instancing example that comes with the newest SDK.

The poster stated that he didn't understand it, and needed some clarification on how software-based instancing actually works.

Share this post

Link to post
Share on other sites
This is a great site to turn to. Here's a psuedo instancing example. The idea behind instancing is to minimize the number of state changes per rendered frame, and thus minimize the number of DrawPrimitive calls. State changes occur when texture or shader (where shader information includes transform information) information varies between groups of objects. It is possible to avoid these changes using a variety of techniques. Texture Atlasing is one such technique. Another way is to pack transform information in per-vertex, either by pre-transforming vertices, or by using texture coordinates to represent transform information.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!