5.2 Buffer Usage

Because the memory in a hardware buffer is likely to be under significant contention during the rendering of a scene, the kind of access you need to the buffer over the time it is used is extremely important; whether you need to update the contents of the buffer regularly, whether you need to be able to read information back from it, these are all important factors to how the graphics card manages the buffer. The method and exact parameters used to create a buffer depends on whether you are creating an index or vertex buffer (See section Hardware Vertex Buffers and See section Hardware Index Buffers), however one creation parameter is common to them both - the ’usage’.

The most optimal type of hardware buffer is one which is not updated often, and is never read from. The usage parameter of createVertexBuffer or createIndexBuffer can be one of the following:

HBU_STATIC

This means you do not need to update the buffer very often, but you might occasionally want to read from it.

HBU_STATIC_WRITE_ONLY

This means you do not need to update the buffer very often, and you do not need to read from it. However, you may read from it’s shadow buffer if you set one up (See section Shadow Buffers). This is the optimal buffer usage setting.

HBU_DYNAMIC

This means you expect to update the buffer often, and that you may wish to read from it. This is the least optimal buffer setting.

HBU_DYNAMIC_WRITE_ONLY

This means you expect to update the buffer often, but that you never want to read from it. However, you may read from it’s shadow buffer if you set one up (See section Shadow Buffers). If you use this option, and replace the entire contents of the buffer every frame, then you should use HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE instead, since that has better performance characteristics on some platforms.

HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE

This means that you expect to replace the entire contents of the buffer on an extremely regular basis, most likely every frame. By selecting this option, you free the system up from having to be concerned about losing the existing contents of the buffer at any time, because if it does lose them, you will be replacing them next frame anyway. On some platforms this can make a significant performance difference, so you should try to use this whenever you have a buffer you need to update regularly. Note that if you create a buffer this way, you should use the HBL_DISCARD flag when locking the contents of it for writing.

Choosing the usage of your buffers carefully is important to getting optimal performance out of your geometry. If you have a situation where you need to update a vertex buffer often, consider whether you actually need to update all the parts of it, or just some. If it’s the latter, consider using more than one buffer, with only the data you need to modify in the HBU_DYNAMIC buffer.

Always try to use the _WRITE_ONLY forms. This just means that you cannot read directly from the hardware buffer, which is good practice because reading from hardware buffers is very slow. If you really need to read data back, use a shadow buffer, described in the next section.