locked
Can't prevent multiple threads from calling DirectX

    Question

  • Hi,

    I have a SharpDX application, UAP, Windows 10 Build 10049, It looks like it's targeting Windows 8.1 though (no choice).

    I'm integrating XAML and DirectX using the SwapChainPanel and development was proceeding normally. But over the past 24 hours, i have experienced numerous and unpredictable crashes giving the following error:

    D3D11 CORRUPTION: ID3D11DeviceContext::ClearDepthStencilView: Two threads were found to be executing functions associated with the same Device[Context] at the same time. This will cause corruption of memory. Appropriate thread synchronization needs to occur external to the Direct3D API (or through the ID3D10Multithread interface). 1048 and 5940 are the implicated thread ids. [ MISCELLANEOUS CORRUPTION #28: CORRUPTED_MULTITHREADING]

    Now it seems to be occurring after i add code to update a texture on the gpu. However, similar code was running yesterday just fine. I don't know what's changed, and I actually don't think any changes are the problem.

    I've read that CompositionTarget_Rendering can fire more than once per frame, so i added the following code to my rendering method:

            private void CompositionTarget_Rendering(object sender, object e)
            {
                RenderingEventArgs rea = (RenderingEventArgs)e;
                if (rea.RenderingTime == lastRenderCallTime)
                    return;
                lastRenderCallTime = rea.RenderingTime;

    I would think that this should prevent the method from running twice, but obviously it's not working. Edit 4 : this is actually working, i see the method starting, and the body not executing over the first 2 or so frames.

    Edit: The texture update doesn't actually get called until an edit has occurred. So that doesn't seem to be causing the frames cost to blow out.

    Edit 2: It turns out that if i slow the rendering method down, it crashes less or not at all. So if i add some some small amount of work before the directx work, i avoid the issue. Strange but my thought that it's a timing issue with the rendering call seems to be confirmed. And slow code is not the problem.

    Edit 3: With a debug message at the start and end of the rendering call:
    Render.Start
    Render.Start
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    Render.End
    Render.Start
    D3D11 CORRUPTION: ID3D11DeviceContext::ClearDepthStencilView: Two threads were found to be executing functions associated with the same Device[Context] at the same time.

    Any ideas are appreciated.





    Sunday, April 5, 2015 7:25 PM

Answers

  • Ah, I think i have fixed this. Because I'm loading my game assets asynchronously, as I added more assets to my project the loading process started to occasionally overlap the rendering process in the main loop. That explains why it was unpredictable and not evident before yesterday (when i added a texture array load).
    Sunday, April 5, 2015 10:32 PM