I was cleaning up my dynamic vertex and index buffer code, when I started wondering something
Am I locking these buffers right? This is a little hard to explain for me so please let me know what does not make sense
currently I just lock the entire buffer like this:
vBuffer->Lock(0, 0, (void**) &vertices, bufferLockFlag);
iBuffer->Lock(0, 0, (void**) &indices, bufferLockFlag);
But since these are dynamic buffers wouldnt I want to be locking like this?
vBuffer->Lock(vertexDataAmountUsed, vertexBufferSize - vertexDataAmountUsed, (void**) &vertices, bufferLockFlag);
iBuffer->Lock(indexDataAmountUsed, indexBufferSize - indexDataAmountUsed, (void**) &indices, bufferLockFlag);
For example lets say we are:
- Drawing quads
- Each quad has 4 vertices and 6 indices, since we are using a index buffer
- That each quad has a vertex data size of 10
- That each quad has a index data size of 2
- That our vertex buffer can hold 3 quads before it is "full"
- Meaning that our buffers sizes are:
vertexBufferSize: 'Vertex Data size per quad' * 'number of quads the buffer can hold' = 10 * 3 = 30
indexBufferSize: 'Index Data size per quad' * 'number of quads the buffer can hold' = 2 * 3 = 6
- That we are starting with fresh empty buffers
vertexDataAmountUsed = 0;
indexDataAmountUsed = 0;
We lock the buffers like so
vBuffer->Lock(vertexDataAmountUsed, vertexBufferSize - vertexDataAmountUsed, (void**) &vertices, bufferLockFlag);
iBuffer->Lock(indexDataAmountUsed, indexBufferSize - indexDataAmountUsed, (void**) &indices, bufferLockFlag);
Meaning that we are locking the entire buffer for both the index and vertex buffers because:
- vertexDataAmountUsed and indexDataAmountUsed is 0. This tells the lock call to use a 0 offset for the lock (First param in the lock calls)
- We are locking the entire buffers worth for both buffers, based on the amount of data to lock (Second param in the lock calls)
vertexBufferSize - vertexDataAmountUsed = 30 - 0 = 30
indexBufferSize - indexDataAmountUsed = 6 - 0 = 6
So down the line, lets say we do not need a fresh buffer. That we still have room left.
Lets say there are 2 quads worth of data in for each buffer meaning:
vertexDataAmountUsed = 'Vertex data per quad' * 2 = 10 * 2 = 20
indexDataAmountUsed = 'Index data per quad' * 2 = 2 * 2 = 4
So this means we can hold one more quad before we have to use the DISCARD flag to get a fresh buffer
Meaning that when we lock again using
vBuffer->Lock(vertexDataAmountUsed, vertexBufferSize - vertexDataAmountUsed, (void**) &vertices, bufferLockFlag);
iBuffer->Lock(indexDataAmountUsed, indexBufferSize - indexDataAmountUsed, (void**) &indices, bufferLockFlag);
We are locking like so:
- We are starting the lock at the 20 data amount offset mark for the vertex buffer (First param in the vertex buffer lock)
- We are starting the lock at 4 data amount offset mark for the index buffer (First param in the index buffer lock)
- For the vertex buffer we are only locking what we have left available (Second param in the vertex buffer lock). In this case:
vertexBufferSize - vertexDataAmountUsed = 30 - 20 = 10
- For the index buffer we are only locking what we have left available (Second param in the vertex buffer lock). In this case:
indexBufferSize - indexDataAmountUsed = 6 - 4 = 2
Meaning that only the remaining data quad spot left in the buffers was locked
Now, what am I really asking?
Well, I want to know if what I just described above is correct? Is that how I should be locking dynamic vertex and index buffers?
That I can say vertexBufferSize - vertexDataAmountUsed to lock my whole buffer assuming it matches my max vertex buffer size?
I know if you use 0, 0 in the first and second param it locks the whole thing, but can this be used as an alternative?
Or should I just stick with locking the entire thing?