I want to include the drawing of other vectors of vertices in the following code, in a number of places it seems easy enough to simply add or multiply size()s of the two vectors, for instance in the first check. But then it gets a bit fuzzy simply because of a lack of understanding.

Should I add them together to create a bigger buffer description and copy everything together? Or should I do it all separately?

Basically how would I extend this code to include more vectors of vertices?

if (temporary2DVerts.size() > 0)
    // create the vertex buffer and store the pointer into pBuffer, which is created globally
    D3D10_BUFFER_DESC bd;
    bd.Usage = D3D10_USAGE_DYNAMIC;
    bd.ByteWidth = sizeof(VERTEX) * temporary2DVerts.size();// this breaks the create buffer line : temp2DVerts.size();
    bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
    bd.MiscFlags = 0;

    device->CreateBuffer(&bd, NULL, &pBuffer);

    void* pVoid;    // the void pointer

    pBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pVoid);    // map the vertex buffer
    memcpy(pVoid, &temporary2DVerts[0], sizeof(temporary2DVerts[0]) * temporary2DVerts.size());

    // select which input layout we are using

    // select which primtive type we are using

    // select which vertex buffer to display
    UINT stride = sizeof(VERTEX);
    UINT offset = 0;
    device->IASetVertexBuffers(0, 1, &pBuffer, &stride, &offset);

    // apply the appropriate pass

    // draw the vertex buffer to the back buffer
    device->Draw(temporary2DVerts.size(), 0);


EDIT - just to be clearer - there are 4 vectors of vertices. two that don't change and two that constantly change. permanent 2D and permanent 3D and a temporary version of both. This is so the permanent ones can just be loaded in and are always drawn, and the temporary ones only get drawn for a frame. This way I can create moving lines for debugging, like line of sight cones etc.

EDIT - the naive approach would be to duplicate this code 4 times for each vector, but surely there is a better way? I don't think this is a hard thing to do, but trying to do it has exposed a basic gap in my knowledge about what some of these lines of code do. should I just populate one big list from all the vectors and push that through?

some need to be drawn with different passes, and some don't need to be changed each frame. for example, the permanent ones can just stay on the gpu. right?

  • 2
    \$\begingroup\$ Maybe I'm missing the question, but wouldn't you just add all the vertices you wanted into temporary2DVerts then run this code? \$\endgroup\$
    – House
    Commented Jan 12, 2012 at 0:11
  • \$\begingroup\$ I want to be able to clear some every frame, and not others. \$\endgroup\$ Commented Jan 12, 2012 at 6:46
  • 1
    \$\begingroup\$ This seems to be a very localized question.. also it's not so clear what should be achieved. Do you have a constantly changing amount of vertices to draw? \$\endgroup\$
    – bummzack
    Commented Jan 12, 2012 at 7:48
  • \$\begingroup\$ sorry that is unclear. I'll edit the question. \$\endgroup\$ Commented Jan 12, 2012 at 16:59

1 Answer 1


First of all, you shouldn't create buffers at rendering time as it is a costly operation, putting pressure on the GPU memory (it is also not clear that you are actually releasing the vertex buffer). It is better to allocate buffers at the beginning of your application.

If I understand your use case, you have a set of non-moving vertices-lines and a set of moving vertices-lines: It is better to allocate two buffers for each usage, one that is not declared as dynamic and the other dynamic.

You should first draw your buffer of constant vertices. Then for the moving lines, you just have to update the other specific dynamic buffer on each frame. This buffer should be allocated with enough space for the maximum number of expected vertices to move. If number of vertices could change heavily between the calls (ranging from 10 to 10000), you could create 2-3 dynamic buffers for the right maximum size (for example 10, 1000, 10000) and select the right dynamic buffer according to your temporary2DVerts.size();.

  • \$\begingroup\$ I don't think the number would change particularly heavily. difference of a few dozen at most. Would you be able to show me how to set up and handle a dynamic buffer? or if that's too much of an ask just point me in the right direction. \$\endgroup\$ Commented Jan 14, 2012 at 9:07
  • \$\begingroup\$ In your example above, you are already declaring a dynamic buffer (with D3D10_USAGE_DYNAMIC) and using it (Map/UnMap). \$\endgroup\$
    – xoofx
    Commented Jan 14, 2012 at 10:18
  • \$\begingroup\$ Just move the create declaration outside your rendering loop, declare it with enough space and just keep your Map/UnMap methods. But if you really have only in total a dozen of static and dynamic lines, you shouldn't bother and use a single dynamic buffer directly. It's not really critical in this case. \$\endgroup\$
    – xoofx
    Commented Jan 14, 2012 at 10:26
  • \$\begingroup\$ well I have a lot of static lines, just a few dynamic lines. But what would be the alternative anyway? I'm basically building on the knowledge grained from one tutorial so I don't exactly have what you might call a good working knowledge of directx graphics code. \$\endgroup\$ Commented Jan 14, 2012 at 10:45
  • \$\begingroup\$ If you have lots of static lines, create your dynamic vertex buffer (same than in your sample) and create a "static" vertex buffer at the beginning of your application with D3D10_USAGE_DEFAULT (and no cpu access..etc.), upload your static vertices just once to the GPU using ID3D10Device::UpdateSubresource. Then in your rendering loop, draw your static lines using the static vertex buffer (with IASetInputLayout/IASetPrimitiveTopology/IASetVertexBuffers/Apply/Draw sequence) and then Map/UnMap your dynamic lines and draw them, using basically the same sequence but now with the dynamic buffer. \$\endgroup\$
    – xoofx
    Commented Jan 14, 2012 at 12:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .