Skip to main content
GameDev.net gamedev.net
🔒 Locked

Graphics glitches when using 2 threads.

Started by Gnollrunner Nov 1, 2018 at 6:18 PM 3 replies 1.4k views
Original Post
Gnollrunner
Gnollrunner

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.

dthall43
dthall43

Are you certain that your mesh build thread is not using the same section of memory in the CPU as the previous build, causing part of the previous mesh to become corrupted before it can be copied to DX11.

Gnollrunner
Gnollrunner
Just now, dthall43 said:

Are you certain that your mesh build thread is not using the same section of memory in the CPU as the previous build, causing part of the previous mesh to become corrupted before it can be copied to DX11.

The way the mesh format is it would crash if it had any serious problems. It has faces, edges, vertexes, reference counts many pointers, yada yada.......... On top of that I wrote a subroutine just to check it and I call that right before the copy section. All the meshes pass with flying colors. I figure it has to be something connected to DX11, especially since a mutex ostensibly fixes the problem.

Gnollrunner
Gnollrunner


Update .......... OMG! I found it after about 10 hours of trying. You just have to read the right ? Microsoft doc page.........

https://docs.microsoft.com/en-us/windows/desktop/direct3d11/overviews-direct3d-11-render-multi-thread-intro

" Only one thread at a time should use the immediate context. However, your application should also use that same thread for Microsoft DirectX Graphics Infrastructure (DXGI) operations, especially when the application makes calls to the IDXGISwapChain::Present method. "

I wasn't guarding the swap chain present because you technically don't pass it the graphics context. I'd like to say I should have guessed, but I never would have guessed ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.