# Problem with ID3D11DeviceContext::Map()

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

## Recommended Posts

I want to update the content in my vertex buffer, but the call to ID3D11DeviceContext::Map() fails and returns E_INVALIDARG. This is how I create the vertex buffer:

template <class VertexType>
void Primitive::SetVertices(ID3D11Device* device, vector<VertexType> vertices, int size)
{
// Fill out the D3D11_BUFFER_DESC struct.
D3D11_BUFFER_DESC vbd;
vbd.Usage = D3D11_USAGE_DYNAMIC;
vbd.ByteWidth = sizeof(VertexType) * size;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vbd.MiscFlags = 0;
// Set the init data.
D3D11_SUBRESOURCE_DATA initData;
initData.pSysMem = &vertices[0];
// Create the vertex buffer.
HR(device->CreateBuffer(&vbd, &initData, &mVertexBuffer));
mNumVertices = size;
}
[/quote]

And this is the attempt to update the vertex buffer:

void Terrain::UpdateVertices()
{
ID3D11DeviceContext* context = GetD3DContext();
D3D11_MAPPED_SUBRESOURCE resource;
HRESULT hr = context->Map(mPrimitive->GetVertices(), 0, D3D11_MAP_WRITE, 0, &resource);

Vertex* vertices = (Vertex*)resource.pData;
for(int i = 0; i < mPrimitive->NumVertices(); i++)
{
vertices.Pos.y = GetHeight(vertices->Pos.x, vertices->Pos.z); // Change the vertex Y pos.
}
context->Unmap(mPrimitive->GetVertices(), 0);
}
[/quote]

I get no error if I use D3D11_MAP_WRITE_DISCARD instead. I'm not even sure if my approach to update the vertex buffer is correct in this situation. It's a terrain editor where the user will be able to use tools to change the terrain. It feels unneccessary to update every vertex in the terrain when the tools only will effect a certain radius. Is UpdateSubresource() better in this case?

Anyways, I want to get it working with Map() so I then can compare the performance. Thanks for helping

EDIT: The code tags are messing with me... Edited by simpler

##### Share on other sites
From what I can see you set the writeflag, but not the readflag on the vertexbuffer description, the same with the map function, you set the write flag but not the readflag (D3D11_MAP_READ), i.e. in your case you are allowed to write to the buffer, but not read from it.

##### Share on other sites
If I set vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ then ID3D11Device::CreateBuffer() doesn't work, which I don't understand. Edited by simpler

##### Share on other sites
I think the easiest way is to keep the vertices in mainmemory and then make the update against that and send the updated relevant parts via map/updatesubresource with write discard as MJP wrote below. Edited by zerorepent

##### Share on other sites
You want to use DISCARD in this case, and completely fill the entire buffer with new data. Doing it this way allows the driver to avoid synchronization between the CPU and GPU.

##### Share on other sites
Just checked the D3D documentation, and can find no indication that USAGE_DYNAMIC only works with WRITE_DISCARD or WRITE_NO_OVERWRITE, but that definitely seems to be the symptom you have. Just do what MJP said and use discard.

##### Share on other sites
Thanks for the answers! I got it working now with D3D11_MAP_WRITE_DISCARD but it's really slow since the entire terrain vertex buffer has to be built each frame when the terrain tool is used. It's a very small part of the terrain that needs to get updated so there got to be a better way to do this, being able to not use DISCARD would probably be the best solution. Then I only have to change the vertices that gets affected. Or do you have any other suggestions?

Right now I'm doing this every frame with the grid size being 257x257

 // Get the vertices. ID3D11DeviceContext* context = GetD3DContext(); D3D11_MAPPED_SUBRESOURCE resource; HRESULT hr = context->Map(mPrimitive->GetVertices(), 0, D3D11_MAP_WRITE_DISCARD, 0, &resource); Vertex* v = reinterpret_cast<Vertex*>(resource.pData); for(UINT i = 0; i < m; ++i) { float z = halfDepth - i*dz; for(UINT j = 0; j < n; ++j) { float x = -halfWidth + j*dx; v[i*n+j].Pos = XMFLOAT3(x, GetHeight(x, z), z);//GetHeight(x, z) v[i*n+j].Normal = XMFLOAT3(0, 1, 0); v[i*n+j].Tangent = XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f); v[i*n+j].Tex.x = j*du; v[i*n+j].Tex.y = i*dv; //mVertices[i*n+j] = v[i*n+j]; } } context->Unmap(mPrimitive->GetVertices(), 0); 

And it doesn't feel that optimal. Edited by simpler

##### Share on other sites
Have a buffer in memory; calculate changes into that then map and memcpy into the vertex buffer.

The change calculation + map/copy/unmap shouldn't happen every frame, only when data has been changed.

##### Share on other sites

Have a buffer in memory; calculate changes into that then map and memcpy into the vertex buffer.

I should've thought about that, thanks a lot! Edited by simpler

##### Share on other sites
Another option for this is UpdateSubresource.

In the normal use case that's not recommended, but for updating smaller areas of buffers (or textures) without doing a full discard/rebuild it does present certain advantages, namely that it will manage GPU/CPU resource contention for you, at the cost of some extra memory copies. You'll need to create with usage default and no CPU access, but I think it's worth a try and it won't upset your current code too much.

Watch out for the D3D11_BOX struct - left is the start of your update region (in bytes), right the end, top is 0, bottom 1, front 0 and back 1.

##### Share on other sites
The D3D11_USAGE enumeration lists available access for different resources:
http://msdn.microsoft.com/en-us/library/windows/desktop/ff476259%28v=vs.85%29.aspx

You definitely want all those vertices in Default usage, as most of them are only used by the GPU.

##### Share on other sites

The D3D11_USAGE enumeration lists available access for different resources:
http://msdn.microsof...9(v=vs.85).aspx

You definitely want all those vertices in Default usage, as most of them are only used by the GPU.

But having them in default usage means that I can't use the ID3D11Device::Map(). Should I use UpdateSubresource() as mhagain mentioned above in that case?

##### Share on other sites
Yes. I recommend reading through the entire page I linked. It talks about tradeoffs from the different types, and when to use them. Edited by Erik Rufelt