DX11 debug layer fails with CORRUPTED_MULTITHREADING when encoding using asynchronous method

Following the guidelines in the documentation, and due to wanting to get the absolute minimum latency out of the system as possible, I moved to asynchronous mode on Windows 10.

When encoding and then waiting for the completion event and locking output buffers on the same thread, my code works fine.

When I move the code which waits on the completion event and then calls nvEncLockBitstream and nvEncUnlockBitstream, I was getting unpredictable crashes in nvwgf2umx.dll. I turned on the debug runtime, and the following message was shown:

D3D11 CORRUPTION: ID3D11DeviceContext::IASetPrimitiveTopology: 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). 19248 and 14720 are the implicated thread ids. [ MISCELLANEOUS CORRUPTION #28: CORRUPTED_MULTITHREADING]
D3D11: **BREAK** enabled for the previous message, which was: [ CORRUPTION MISCELLANEOUS #28: CORRUPTED_MULTITHREADING ]

This usually occurs while within the call to nvencLockBitstream, and other times within the call to nvEncUnlockBitstream. This occurs on the secondary non-render thread, as per the documentation. I’ve verified that the main thread is not attempting to access the input resource that was being processed by the encoder.

The callstack of the call into nvencLockBitstream is as follows:

ntdll.dll!00007ff88f82c034() Unknown
KernelBase.dll!00007ff88d2aa34e() Unknown
nvwgf2umx.dll!00007ff8505064b6() Unknown
nvwgf2umx.dll!00007ff8505075b0() Unknown
nvwgf2umx.dll!00007ff8505bdc4a() Unknown
d3d11.dll!CDecodeContext::DecodeExtension() Unknown
d3d11.dll!CContext::DecoderExtension() Unknown
d3d11_3SDKLayers.dll!00007ff81325fbff() Unknown
nvEncodeAPI64.dll!00007ff84a0eca80() Unknown
nvEncodeAPI64.dll!00007ff84a0edef5() Unknown
nvEncodeAPI64.dll!00007ff84a0fb3aa() Unknown
nvEncodeAPI64.dll!00007ff84a10fadf() Unknown

A few years ago, a similar thread was brought up on this forum, and the solution was to move back to a single thread. DirectX 11 and asynchronous encoding I wonder if asynchronous receipt of the encoded packets is actually not supported? Do we have to use CUDA perhaps? It’s really not clear, and there don’t seem to be any examples of this approach.

I reworked my application to convert all DX11 objects into CUDA objects for consumption by the NVENC library.

After this work, the stability issues went away.

It seems as if DX11 encoder devices do not work when attempting to read the locked bitstream on another thread.