Over the last two months I have been converting my engine from 64-bit single threaded to 32-bit multithreaded. The decision for multithreading is for various performance reasons. The decision for 32-bit is because I use Chromium Embedded Framework to power HTML (and CSS, Flash, Java, etc...) based user interfaces. I had originally wrapped CEF in a separate 32-bit process and used shared memory so that it could communicate with my 64-bit engine. I believe that this was the cause of false positives from my anti-virus software so I decided that going 32-bit to use CEF directly was a justifiable option.
CEF creates multiple threads internally. One of these threads calls an OnPaint() function when it is necessary to update the user interface image data. Inside OnPaint(), I call m_pImmediateContext->Map() with D3D11_MAP_WRITE_DISCARD in order to update the user interface texture. This, obviously, is giving me problems...
D3D11: CORRUPTION: ID3D11DeviceContext::Map: Two threads were found to be executing functions associated with the same Device at the same time. This will cause corruption of memory. Appropriate thread synchronization needs to occur external to the Direct3D API. 66768 and 67180 are the implicated thread ids. [ MISCELLANEOUS CORRUPTION #28: CORRUPTED_MULTITHREADING ]
My first thought was to simply lock the immediate context before calling Map(). However, my user interface is rendered on it's own dedicated deferred context (concurrently with all other deferred contexts). Specifically, a command list is generated (on the user interface deferred context) for rendering the user interface overlay to the screen on start up. That command list is reused for the life of the program (since the sequence of commands for rendering the user interface does not change), leaving the user interface's deferred context unused, yet available. In theory, if I can use this unused context to update the texture, I should be able to avoid having lock the immediate context. It would require quite a bit of rewriting just to be able to test this theory, so I decided to ask before hand to avoid doing so in vain.
Can ID3D11DeviceContext::Map() be called on a deferred context and provide immediate access to subresource data?
I apologize for taking so long to get to the question. I just wanted explain my intent and what led me to this question.