Resizing the Vertex Buffer

Started by
2 comments, last by hplus0603 18 years, 7 months ago
I'm using DirectX SDK 9.0c August 2005. I have a class which is in control of drawing line lists and line strips, yet as vertices are added to the class (therefore extending the number of lines that are drawn), the vertex buffer's internal space must be expanded to hold the additional vertices. Is a constant call every frame to CreateVertexBuffer slow? This seems the only way to adjust the size of the Vertex Buffer is to re-create it every time vertices are added. If there is another way I would love to know about it. Thanks!
Advertisement
Creating a new vertex buffer every frame is slow. Try using a dynamic vertex buffer and filling that every time with (subsections of) the actual data.
Other possibilities are callbacks (even slower) or DrawPrimitiveUP (which also creates a temporary vertex buffer, but may be a bit more optimised by the runtime because the usage pattern is known). Each of these methods mean you hold a copy of the vertex data somewhere in your app's memory.

That you want to resize a buffer implies that you have a buffer with valid data and simply want to add more. Then why not just create a new larger buffer only when you do add data. Don't bother shrinking it.
Kippesoep
Better yet, screw the vertex buffer, render your primitives using IDirect3DDevice9::DrawPrimitiveUP. Use it when you don't want to use (or can't) use vertex buffers. It's similar to Direct3D Immediate Mode's old IDirect3DDevice7::DrawPrimitive. But depending on how you use it, you are giving up efficiency, so be careful.

Click here
DrawPrimitiveUP is typically slower than a custom solution you make yourself. Calling CreateVertexBuffer every frame is slow, too, though.

Of course, when we say "slow" we might mean the difference between 290 and 270 FPS, not the difference between 60 and 30 fps, if it's only for a single vertex buffer.

Depending on how often you add vertices, I would either use a DYNAMIC vertex buffer for NOOVERWRITE/DISCARD locking (a la vertex streaming), or I would allocate a bigger vertex buffer (say, rounded up to the nearest 128 vertices), and resize it upwards only when it actually gets full.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement