• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
simpler

Problem with ID3D11DeviceContext::Map()

12 posts in this topic

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:

[quote]
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:

[quote]
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);
}
[/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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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

Share this post


Link to post
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.
1

Share this post


Link to post
Share on other sites
If I set [i]vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ[/i] then ID3D11Device::CreateBuffer() doesn't work, which I don't understand. Edited by simpler
0

Share this post


Link to post
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
2

Share this post


Link to post
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.
1

Share this post


Link to post
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

[CODE]
// 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);
[/CODE]

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

Share this post


Link to post
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.
1

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1351121042' post='4993591']
Have a buffer in memory; calculate changes into that then map and memcpy into the vertex buffer.
[/quote]

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

Share this post


Link to post
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.
2

Share this post


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

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

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1351157989' post='4993734']
The D3D11_USAGE enumeration lists available access for different resources:
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/ff476259%28v=vs.85%29.aspx"]http://msdn.microsof...9(v=vs.85).aspx[/url]

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

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?
0

Share this post


Link to post
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
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0