Using the Vertex and Index Buffers

Vertex buffers are the main data structure used by Direct3D to store vertices, while the Index buffers are the same for indices. These classes derive from the Resource class, and thus inherit those methods, plus add a few of their own.

The first new property is a description of the buffer. The structure returned from this property will tell you everything you need to know about how the buffer was created, including the format, the usage, the memory pool, the size, and the vertex format. While it may be true that you would already know this information if you were creating the buffers yourself, this is very useful information if you are simply receiving the buffers from an external source that you know nothing about.

SHOP TALK: STATIC AND DYNAMIC BUFFERS

Before getting into the last two methods that reside on the buffers, now seems like the perfect time to discuss the differences between static and dynamic buffers. The concepts here apply equally well to both vertex and index buffers.

So, obviously the first question would be, "What is the difference between a static and a dynamic buffer?" As luck would have it, I happen to have an answer for that. If the buffer was created with the usage flag Usage.Dynamic, the buffer is a dynamic buffer; any other buffer is a static buffer. Static buffers are designed for items that do not change very often, while dynamic buffers are more easily used for buffers that have constantly changing data. You can see where the names come from.

Just because a buffer is static, though, doesn't mean it cannot be modified. However, locking a static buffer while that buffer is already in use can have a very significant performance penalty. Before the data can be modified, the GPU needs to finish reading the current set of data, which can be very time consuming. Doing this several times in the same frame is even worse, because now you've eliminated the buffering the driver can do, and you are forcing your GPU to sit around idling waiting for you to finish modifying your buffer's data. Graphics cards are extremely powerful today; you do not want them idling when they could be busy drawing triangles.

If you expect your buffer's data to be modified frequently, it should be created with Usage.Dynamic. This flag allows Direct3D to optimize the pipeline for frequent data modification. You will receive no speed enhancements by specifying a buffer as dynamic, and then never modifying it; as a matter of fact, it will be slower than if you simply made a static buffer to begin with.

Always know the amount of "churn" you expect on your buffers per frame and create accordingly. For maximum performance, it is recommended that you have one large static vertex buffer for each vertex format you wish to render. It's sometimes not feasible to have only these static buffers, though, and in those cases, the dynamic buffers are there for you.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net