Rendering everything with a shader when only squares are needed?

I am building a game that is tile-based (filled with glowy vector squares). Right now, I am considering using instancing for drawing these, but I would like to know if there is a faster approach.

The squares are all of the same size. They should be rendered in a rectangular grid, and I know exactly how many of them I should render. The squares are also made with linelists (so 8 vertices for a single rectangle).

Knowing all this, is instancing still the best approach? Or can I utilize the GPU more here? (in my game, I only utilize 1% of the GPU, but 100% of the CPU).

You are probably hitting 100% CPU because you are at the batch limit. You get a few thousand* batches per frame. A batch is basically equivalent to a call to one of the GraphicsDevice.Draw* functions.

While you can reduce the number of batches with instancing, because your geometry is so simple you are probably better off using DynamicVertexBuffer or DrawUserPrimatives to put all your geometry into a single batch. (This is the technique that SpriteBatch uses.)

For such a small amount of geometry, the cost of calculating and sending the vertex data to the GPU will be comparable to, and quite possibly better than, the cost of transmitting the instancing data.

Additionally, if your data doesn't change every frame, you should use DynamicVertexBuffer and instead of refreshing it every frame, add a ContentLost handler and then only refresh it when you need to or if the device is lost.

Obviously, if your data never changes at all you should simply build it into a single, static VertexBuffer. If what you are drawing is terrain, and it is not deformable, just have one (or more) huge (thousands of vertices) vertex buffer that you can scroll around, and let the GPU handle culling for you.

You can use geometry shader and send just one point and normal for each rectangle. In geometry shader produce for example triangle strip. But if you need texture coordinates, it could be problem.

// Deleted some Opengl info (now I have taken note that you are talking about XNA :) )

• Nice answer. Also i dont think that using geometry shaders in a way you say will have problems with tex coords ;) – Notabene Apr 21 '11 at 9:14