Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem with ID3D11DeviceContext::Map()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 24 October 2012 - 07:57 AM

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;
}


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[i].Pos.y = GetHeight(vertices->Pos.x, vertices->Pos.z); // Change the vertex Y pos.
}
context->Unmap(mPrimitive->GetVertices(), 0);
}


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 Posted Image

EDIT: The code tags are messing with me...

Edited by simpler, 24 October 2012 - 08:04 AM.


Sponsor:

#2 zerorepent   Members   -  Reputation: 663

Like
1Likes
Like

Posted 24 October 2012 - 08:13 AM

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.

#3 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 24 October 2012 - 10:15 AM

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, 24 October 2012 - 10:19 AM.


#4 zerorepent   Members   -  Reputation: 663

Like
2Likes
Like

Posted 24 October 2012 - 12:04 PM

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, 24 October 2012 - 12:18 PM.


#5 MJP   Moderators   -  Reputation: 10277

Like
3Likes
Like

Posted 24 October 2012 - 12:15 PM

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.

#6 mhagain   Crossbones+   -  Reputation: 7467

Like
1Likes
Like

Posted 24 October 2012 - 01:40 PM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#7 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 24 October 2012 - 05:06 PM

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, 24 October 2012 - 05:06 PM.


#8 phantom   Moderators   -  Reputation: 6804

Like
1Likes
Like

Posted 24 October 2012 - 05:24 PM

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.

#9 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 24 October 2012 - 06:00 PM

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, 24 October 2012 - 06:08 PM.


#10 mhagain   Crossbones+   -  Reputation: 7467

Like
2Likes
Like

Posted 24 October 2012 - 08:19 PM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#11 Erik Rufelt   Crossbones+   -  Reputation: 3108

Like
1Likes
Like

Posted 25 October 2012 - 03:39 AM

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.

#12 simpler   Members   -  Reputation: 913

Like
0Likes
Like

Posted 25 October 2012 - 04:37 AM

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?

#13 Erik Rufelt   Crossbones+   -  Reputation: 3108

Like
1Likes
Like

Posted 25 October 2012 - 05:03 AM

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, 25 October 2012 - 05:05 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS