Microsoft DirectX 8.1 (Visual Basic)

Accessing the Contents of a Vertex Buffer

Vertex buffers enable Microsoft® Visual Basic® applications to easily update the vertex data they contain. To lock, fill, and unlock vertex buffers use the D3DVertexBuffer8SetData helper function. D3DVertexBuffer8SetData accepts five parameters, The first, Vbuffer, is the Direct3DVertexBuffer8 object that contains the vertex data. The second parameter, OffsetToLock, is the offset into the vertex data. The third parameter is the size, measured in bytes, of the vertex data.

The fourth parameter, Flags, tells the system how the memory should be locked. You can use it to indicate how the application will access the data in the buffer. Specify constants for the Flags parameter according to the way the vertex data will be accessed by your application. This allows the driver to lock the memory and provide the best performance given the requested access type. Use D3DLOCK_READONLY flag if your application will read only from the vertex buffer memory. Including this flag enables Microsoft® Direct3D® to optimize its internal procedures to improve efficiency, given that access to the memory will be read-only.

The last parameter accepted by the D3DVertexBuffer8SetData helper function, Data, is the first element of an array of data to be loaded into the vertex buffer. This parameter is of type Any. To use it properly you must specify the first element of the array for Data, as shown in the following code example.

Dim Vertices(3) As CUSTOMVERTEX
Dim VertexSizeInBytes As Long

' Determine the length of the vertex data.
VertexSizeInBytes = Len(Vertices(0))

' Fill the vertices with data.
       .
       .
       .

' This code example assumes that g_VB is a variable containing a 
' valid reference to a Direct3DVertexBuffer8 object.

' VBuffer=g_VB             The vertex buffer to fill with data.
' Offset=0                 Start filling from the start of the buffer.
' Size=VertSizeInBytes*3   Copy three CUSTOMVERTEX types to the buffer.
' Flags=0                  Send default flags to the lock operation.
' data=Vertices(0)         This parameter is of type Any. In order 
'                          to use it, send the first element in 
'                          the array.

D3DVertexBuffer8SetData g_VB, 0, VertexSizeInBytes * 3, 0, Vertices(0)

The vertex buffer memory is a simple array of vertices specified in flexible vertex format. The number of texture coordinates present in the vertex format is described by the D3DFVF_TEXn flags, where n is a value from 0 to 8. Multiply the number of texture coordinate sets by the size of one set of texture coordinates, which can range from one to four floats, to calculate the memory required for that number of texture coordinates.

Performance Notes  If you create a vertex buffer with the D3DUSAGE_WRITEONLY flag, do not use the D3DLOCK_READONLY locking flag. Use the D3DLOCK_READONLY flag if your application will read only from the vertex buffer memory. Including this flag enables Direct3D to optimize its internal procedures, given that access to the memory will be read-only.

See Using Dynamic Vertex and Index Buffers for information on using D3DLOCK_DISCARD or D3DLOCK_NOOVERWRITE for the Flags parameter of the Lock method.

To lock, read, and unlock the contents of vertex buffers, use the D3DVertexBuffer8GetData helper function.

Retrieving Vertex Buffer Descriptions

Retrieve information about a vertex buffer by calling the Direct3DVertexBuffer8.GetDesc method. This method fills the members of the D3DVERTEXBUFFER_DESC structure with information about the vertex buffer.