Saturday, June 23, 2012

Animations using Instancing and Compute Shaders

The concept of Instancing was brought about in DX9, where using the same mesh you could render it again. Compute shaders was brought about when DX11 was introduced as a way to use the GPU to process non-graphics computations.

I decided that I wanted to use these features for animating my meshes. Instancing was useful because I wanted to render the same mesh in different places without using different draw calls for each mesh. And Compute shaders was very useful for computing the positions of the bones. In this case, I wanted to perform the bone computations for all the meshes in the same scene at the same time.

The use of unordered access buffers in the compute shaders made it possible for me to define a structure for the update of bones. I used this to update the bones of all the meshes.

And the use of structured buffers in the pixel shaders made it possible to read the data in anyway we wanted it to be.

In this case, I used the instance ID of the mesh to index into the bones buffer. And with GPU Skinning, the meshes are all animated

So with the combination of compute shaders for the update of skeletons, and instancing to render all the meshes alongwith their animations, I was able to animate and render 101 meshes with just one update and one draw call.

Following is a video of the result.

One major drawback for this method is that we have to load all the keyframes into the GPU when performing the Update for the skeleton.

No comments: