• 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
CrashyCartman

ID3D11Device::Map, or how to make fast updates of vertex buffers

11 posts in this topic

Hi gamedev,

I'm requesting your help as I know there is a lot of D3D11 experts here.

First, let me tell you about the background that led me to create this topic:

I use Ogre for the graphic part of my game engine, and Ogre has a nice heightfield module that allows dynamic sculpting of the terrain.
However, if the sculpting (and, in the background, the update of the vertex buffer) is fast enough in DirectX 9, it is way too much slow in DirectX 11, and I would like to improve its speed.

Basically, here is the global setup of the buffers:[list=1]
[*]The terrain hardware buffer is created with an usage==D3D11_USAGE_DEFAULT and CPUAccessFlags==0
[*]At initialisation:
[/list][list]
[*]A staging buffer is created with an usage==D3D11_USAGE_STAGING and CPUAccessFlags == D3D11_CPU_ACCESS_WRITE|3D11_CPU_ACCESS_READ
[/list][list]
[*]The staging buffer is mapped with flags D3D11_MAP_WRITE to get a pointer to the vertex data.
[*]vertex data is written at the previously given location.
[*]Stating buffer is unmapped.
[*]Using ID3D11Device::GetImmediateContext()::CopyResource the vertex position is copied from the staging buffer to the original terrain vertex buffer.
[*]Stating buffer is deleted.
[/list]
When editing the terrain dynamically, it is the same global process described in "2" that is made, but after some debugging I've seen that the ID3D11Device::Map call on the staging buffer is really slow (basically using the "break all" technique to see where the program is spending time.) During this operation, the whole original buffer is updated which is not performance friendly, but the same was done in Direct3d 9 without a huge performance hit.

I know that Direct3D 11 api is really different from the Direct3D 9 one, but is there a way to achieve fast update of a vertex buffer like with the old IDirect3DVertexBuffer9::Lock/Unlock ?

I'm sorry if it's not really clear, english is not my native language and I'm still beginning with all this D3D11 stuff.
If you need more details, please ask.


Thanks.
0

Share this post


Link to post
Share on other sites
I'm beginner myself, but I could suggest to try to create buffer with D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE flags, and store dublicate heightmap data in RAM. Most likely that will double memory usage.

When you need to update heightmap use Map() and then memcpy() from RAM into buffer. I've been using this method for my tool and I see no FPS difference even if I update it every frame.
0

Share this post


Link to post
Share on other sites
Yes but doing it like that means that when rendering the terrain in game mode(==no real time editing), it's going to be slower, no?
0

Share this post


Link to post
Share on other sites
You can edit, why not? Just use Map() and upload new data. You can't read it though, that's why you have to save duplicate data in RAM.
0

Share this post


Link to post
Share on other sites
Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.

Also you can map only a certain region of a buffer so you don't need to copy everything.

You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.
1

Share this post


Link to post
Share on other sites
[quote name='n3Xus' timestamp='1343814118' post='4965155']
Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.
[/quote]
The staging buffer is created at each update, yes.

[quote]
Also you can map only a certain region of a buffer so you don't need to copy everything.
[/quote]
Yes, but the implementation of the terrain in ogre always "lock" the whole buffer, even when editing a small part of the terrain. It's non optimal but is fast enough in Direct3D9. If I cannot achieve to speed up in Direct3d 11, I'll rewrite this code to update only the edited part.
[quote]
You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.
[/quote]
Related to previous quote.
0

Share this post


Link to post
Share on other sites
[quote name='n3Xus' timestamp='1343814118' post='4965155']
Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.
[/quote]
The staging buffer is created at each update, yes.

[quote]
Also you can map only a certain region of a buffer so you don't need to copy everything.
[/quote]
Yes, but the implementation of the terrain in ogre always "lock" the whole buffer, even when editing a small part of the terrain. It's non optimal but is fast enough in Direct3D9. If I cannot achieve to speed up in Direct3d 11, I'll rewrite this code to update only the edited part.
[quote]
You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.
[/quote]
Related to previous quote.


[quote]
I'm beginner myself, but I could suggest to try to create buffer with D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE flags, and store dublicate heightmap data in RAM. Most likely that will double memory usage.[/quote]

I tried doing like that, with D3D11_CPU_ACCESS_WRITE and D3D11_USAGE_DYNAMIC, using a shadow buffer and mapping the buffer with D3D11_MAP_WRITE_DISCARD. There is still huge latency when updating terrain geometry. Edited by CrashyCartman
0

Share this post


Link to post
Share on other sites
[quote]Are you creating the buffer every frame in which you edit your terrain? If so, don't do that.[/quote]

That was the point! Now I create and fill the staging buffer only at the first edition and terrain editing is fast:)

Thanks for your help, I must now find a way to integrate it properly in Ogre.
0

Share this post


Link to post
Share on other sites
before you do so, as boys have stated, you should(!) always create a static buffer, and if for some reason you need to alter it, do so in a shader. (do not update data of buffer that has been streamed to gpu, use shader to firm it)
0

Share this post


Link to post
Share on other sites
[quote name='CrashyCartman' timestamp='1343809480' post='4965137']
english is not my native language
[/quote]
Your post’s English was better than most native speakers’ English.
The only problem was, “an usage”, which should be, “a usage”.


[quote name='CrashyCartman' timestamp='1343815134' post='4965161']
[quote]
You can also check the ID3D11DeviceContext::CopySubresourceRegion function and check how it performs.
[/quote]
Related to previous quote.
[/quote]
I don’t see how the previous quote (a reminder to the audience: It was about updating the whole buffer at once) has any relevance.

ID3D11DeviceContext::CopySubresourceRegion() was the correct answer. It allows you to update a portion of the buffer, which by default means that it is also possible to update the entire buffer all at once.

Use it and check the performance. It should be fine.
And if not, use double-buffering. It requires double the vertex storage but avoids the stall when the resource you want to modify is in the command buffer waiting to be used by the GPU.


L. Spiro
1

Share this post


Link to post
Share on other sites
Sorry for the long delays, a lot of other problems to resolve :)

CopySubresourceRegion is already used to copy from one buffer to another in Ogre D3D11 implementation. However, when the size to copy is equal to the size of the destination buffer, CopySubresource is used.
I think that CopyResource is as fast as CopySubresourceRegion when copying the whole buffer, isn't it? It would be really weird if it wasn't.
0

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