Ideally I'd like to have multiple worker threads to be able to render to off-screen render targets and then 'transfer' the rendered content to the on-screen target. With hwnd render targets this does not seem to be a problem (msdn has an example of it).
I'm not quite sure how to do it when the on-screen render target is based on DXGI swap chain. As far as I know, I can only have one swap chain per window. Therefore I can only have a single render target based on the swap chain. This implies that on-screen rendering can only be done through that single render target.
If my above assumptions are correct, what is the best way to handle the multi-threaded rendering? Do I need to serialize access to the on-screen target? Should worker threads share a single multi-threaded d2d factory? Can on-screen target's BeginDraw/EndDraw/Present be executed on worker threads (i.e. threads that did not create the on-screen target) if a proper locking mechanism is in place?
I'd appreciate any suggestions. Thanks.
I'm tackling this same issue right now! According to my reading on MSDN, the best way is:
I have not yet got a reliably working multithreaded set of Direct2D functions, even following this, so this is all I know so far - I don't yet know about the other caveats etc that are sure to exist.
Some useful key bits:
There is also this very important warning:
For offscreen render targets, you can create separate D2DFactories and attach it to respective offscreen render targets created using CreateWicBitmapRenderTarget or CreateDxgiSurfaceRenderTarget. While transferring it to the onscreen target, you will have to wait for all the threads to finish and then transfer each off-screen RT to on-screen RT one by one.