4
\$\begingroup\$

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).

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

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.

You probably want to read this answer.

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.

\$\endgroup\$
1
\$\begingroup\$

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 :) )

\$\endgroup\$
3
  • \$\begingroup\$ Nice answer. Also i dont think that using geometry shaders in a way you say will have problems with tex coords ;) \$\endgroup\$
    – Notabene
    Commented Apr 21, 2011 at 9:14
  • \$\begingroup\$ So where can I find more information about these shader types? \$\endgroup\$ Commented Apr 21, 2011 at 11:13
  • 2
    \$\begingroup\$ @Mathias Don't care about it. By using XNA you are stucked with shader model 3.0 which hase only vertex and pixel shader... \$\endgroup\$
    – Notabene
    Commented Apr 21, 2011 at 21:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .