Jump to content
  • Advertisement
Sign in to follow this  
Drythe

Fastest way to fill VBuffer every frame?

This topic is 4898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Assuming that the vertex data will change every frame. I've been doing the following code: pDevice->SetStreamSource(0, VertexBuffer , sizeof(MESHVERTEX)); pDevice->SetVertexShader(D3DFVF_MESHVERTEX); MESHVERTEX *MeshVertices; VertexBuffer->Lock( 0, 0, (BYTE**)&MeshVertices, 0); for (int i = 0; i < pMesh->numFaces * 3; i++) { MeshVertices.x = newXValue; MeshVertices.y = newYValue; MeshVertices.z = newZValue; MeshVertices.color = D3DCOLOR_RGBA(r, g, b, a); MeshVertices.tu = tuCoord; MeshVertices.tv = tvCoord; i++; MeshVertices.x = newXValue; MeshVertices.y = newYValue; MeshVertices.z = newZValue; MeshVertices.color = D3DCOLOR_RGBA(r, g, b, a); MeshVertices.tu = tuCoord; MeshVertices.tv = tvCoord; i++; MeshVertices.x = newXValue; MeshVertices.y = newYValue; MeshVertices.z = newZValue; MeshVertices.color = D3DCOLOR_RGBA(r, g, b, a); MeshVertices.tu = tuCoord; MeshVertices.tv = tvCoord; i++; } VertexBuffer->Unlock(); The thing that seems to make this slow is assigning values to the variables in MESHVERTEX struct. I guess it's the overhead of accessing something through struct so many times? Is there a better way programming-wise to fill the buffer with new data?

Share this post


Link to post
Share on other sites
Advertisement
If you aren't already, you should ensure you're creating your vertex buffer with D3DUSAGE_DYNAMIC and use appropriate lock flags - i.e. D3DLOCK_DISCARD and/or D3DLOCK_NOOVERWRITE depending on the scenario.

I suppose your actual filling would be better if you just used memcpy or something similar, but using dynamic VBs is something to check out first.

-Mezz

Share this post


Link to post
Share on other sites
yeah, I have dynamic, but not the flags. Will try that

I actually think my problem is more of a general coding thing... I just don't know how to enter the info any other way. I don't understand how i'd use memcpy for this

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


Somewhere they suggest that you batch your vertices so that while you are filling your data (in a temp array) and copying to the vertex buffer (using a circular buffer scheme) in the GPUs memory. Its supposed to allow the GPU to process data while you are at the same time manipulating the next batch of data in the CPU. I think one of the DirectX SDK samples demonstrates this.

The size of the batches may take experimentation to find optimal performance (long ago it was sets of 500 vertexes, but many times that may be better with later cards).

Share this post


Link to post
Share on other sites
Perhaps it would speed up if you'd access the MeshVertices as a real pointer instead of dereferencing it.


MESHVERTEX *MeshVertices;

VertexBuffer->Lock( 0, 0, (BYTE**)&MeshVertices, D3DLOCK_DISCARD);

for (int i = 0; i < pMesh->numFaces * 3; i++)
{
MeshVertices->x = newXValue;
MeshVertices->y = newYValue;
MeshVertices->z = newZValue;
MeshVertices->color = D3DCOLOR_RGBA(r, g, b, a);
MeshVertices->tu = tuCoord;
MeshVertices->tv = tvCoord;
MeshVertices++;

MeshVertices->x = newXValue;
MeshVertices->y = newYValue;
MeshVertices->z = newZValue;
MeshVertices->color = D3DCOLOR_RGBA(r, g, b, a);
MeshVertices->tu = tuCoord;
MeshVertices->tv = tvCoord;
MeshVertices++;

MeshVertices->x = newXValue;
MeshVertices->y = newYValue;
MeshVertices->z = newZValue;
MeshVertices->color = D3DCOLOR_RGBA(r, g, b, a);
MeshVertices->tu = tuCoord;
MeshVertices->tv = tvCoord;
MeshVertices++;
}

VertexBuffer->Unlock();

Share this post


Link to post
Share on other sites
Might make a difference. That sort of thing's a micro-optimization, though - the only way you can really see which would be faster is to look at the disassembly. When you have to look at the disassembly to solve a problem, it's usually not a problem.

As far as copying into the vertex buffer goes, I'd take a look at the code you have that is generating all the new values. Could you get that stored in a structure with the same layout as a vertex, so that you can just memcpy() one to the other? Could you even write the results directly into the vertex buffer instead of copying them across after the fact? If any values are constant across vertices, could you turn them into vertex shader constant instead? Could you offload some of your per-vertex calculations to the vertex shader?

Also, make sure you're using dynamic vertex buffers instead of regular static ones. Create with dynamic, lock with discard.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!