Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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());
    pBuffer->Unmap();

    // select which input layout we are using
    device->IASetInputLayout(screenVertexLayout);

    // select which primtive type we are using
    device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_LINELIST);

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

    // apply the appropriate pass
    screenPass->Apply(0);

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

    temporary2DVerts.clear();
}

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?

share|improve this question
2  
Maybe I'm missing the question, but wouldn't you just add all the vertices you wanted into temporary2DVerts then run this code? –  Byte56 Jan 12 '12 at 0:11
    
I want to be able to clear some every frame, and not others. –  Asher Einhorn Jan 12 '12 at 6:46
1  
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? –  bummzack Jan 12 '12 at 7:48
    
sorry that is unclear. I'll edit the question. –  Asher Einhorn Jan 12 '12 at 16:59
add comment

1 Answer

up vote 3 down vote accepted
+50

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();.

share|improve this answer
    
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. –  Asher Einhorn Jan 14 '12 at 9:07
    
In your example above, you are already declaring a dynamic buffer (with D3D10_USAGE_DYNAMIC) and using it (Map/UnMap). –  xoofx Jan 14 '12 at 10:18
    
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. –  xoofx Jan 14 '12 at 10:26
    
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. –  Asher Einhorn Jan 14 '12 at 10:45
    
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. –  xoofx Jan 14 '12 at 12:12
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.