Question

What are the thread-safety rules for IDXGISurface1 GetDC/ReleaseDC for textures created with D3D11_RESOURCE_MISC_GDI_COMPATIBLE?

Can I interact with ID3D11Device and ID3D11DeviceContext between GetDC and ReleaseDC on another thread, without data-races or blocking due to implicit synchronization? Or am I in order avoid the GPU idling forced to create a separate D3D11Device for the thread doing GDI rendering and then transferring it back to the "main"D3D11Device by copying to a D3D11_RESOURCE_MISC_SHARED_KEYED_MUTEX?

i.e. is the following valid?

thread 1:

D3D11Device::CreateTexture2D // Create texture 1

IDXGISurface1::GetDC // Get DC for texture 1

... // Draw to texture1 using GDI

IDXGISurtface1::ReleaseDC // Release DC for texture1

thread 2:

// Is this valid if thread 1 is drawing using GDI?

D3D11DeviceContext::OMSetRenderTargets

D3D11DeviceContext::Draw // Draw texture2. 
Was it helpful?

Solution

The following two bullet points from MSDN seems to suggest that not only is this thread unsafe, but there is a reduction in the device's capabilities while an HDC is outstanding.

  • You must release the device(they are referring to the HDC) and call the IDXGISurface1::ReleaseDC method before you issue any new Direct3D commands.

  • This method fails if an outstanding DC has already been created by this method.

Getting an HDC from the DXGISurface inherently involves the surface's parent D3D11 DC. Multi-threaded access of a D3D11 DC is also unsupported. From this MSDN page: If multiple threads must access a single ID3D11DeviceContext, they must use some synchronization mechanism, such as critical sections, to synchronize access to that ID3D11DeviceContext.

I would attempt to prepare the GDI drawing on a seperate thread as you stated, but render it to a system memory buffer. Then simply blit the contents to the DXGI surface.

Also note most of these topics address the behavior of the immediate context, the D3D11 Deferred Context may have different behavior.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top