I'm getting an odd problem with DX11. I kind of solved it but I don't understand why it didn't work the first way. What I'm trying to do is create a bunch of meshes (index and vertex buffers) in one thread but render them in a second thread. I don't don't render the same meshes I'm currently creating. I build a whole set of new meshes and then when everything is ready, the build thread tells the render thread to swap to the new set.
This worked most of the time except for once in a while one of the meshes would be corrupted. It was definitely the mesh generation or copy, and not the render because a corrupted mesh would stick around until the next mesh update, then it would disappear. At first I thought it might be in my CPU side mesh generation code. I build meshes in my own mesh format and then I translate and copy straight to DX11 using ID3D11DeviceContext::map. I am aware that the device context is not thread safe so I guard it with a mutex to make sure I'm not trying to use it in two threads at the same time. Before I did this the program with would simply crash. But afterwards I would only get occasional mesh corruption.
Finally just to try something else I put a mutex around the whole scene render code and then used that same mutex in the other thread around the CPU to DX11 mesh copy section. This solved the problem. However I don't understand why I should be forced to so this since I was protecting the graphics context before. Is there something I'm missing here? Should I even be calling DX11 from more than one thread? Supposedly it's thread safe except for the graphics context.