# How to handle mesh update in its vertex buffer

suppose i have a mesh, it can undergo any kind of changes - bend, stretch, even been torn. How should i update vertex buffer in DirectX for this mesh. For now i recreate it every time, and i don't see any chances for another approach because i don't know what part of vertices array can change between steps.

Is there any known approaches for this problem?

When creating your vertex buffer, specify D3D11_USAGE_DYNAMIC and D3D11_CPU_ACCESS_WRITE in the buffer description (in Usage and CPUAccessFlags members). This will create you a dynamic vertex buffer, which you can update by ID3D11DeviceContext::Map -ping it, copying data to it and ID3D11DeviceContext::Unmap -ping at the end. Something like this:

ID3d11DeviceContext* context;
int dataSize; //size of data to copy
//...

D3D11_MAPPED_SUBRESOURCE mappedResource;
void* dataPtr;
dataPtr = (void*)mappedResource.pData;
memcpy(dataPtr,data,dataSize));
context->Unmap(buffer,0);


Another option is to create a normal vertex buffer with D3D11_USAGE_DEFAULT flag and no cpu access flags and update the buffer by calling ID3D11DeviceContext::UpdateSubResource, but it will be slower.
Like this:

context->UpdateSubresource( buffer, 0, nullptr, data, 0, 0 );


It's called GPU skinning (or a variation of it)

Add several uniforms that represent the changes.

Then add attributes to the vertices that represent how much they are affected by each change.

For example bulging would be going towards the normal vector:

outPos = MVP*(pos_in + bulgeFactor * normal * bulgeUniform)