I\'m writing a ebook reader app for Windows Store. I\'m using Direct2D + DXGI swap chains to render book pages on screen.
My book content sometimes is quite complex (ge
tl;dr: Render to bitmaps on background thread in software mode. Draw from bitmaps to render target on UI thread in hardware mode.
The best approach I've been able to find so far is to use background threads with software rendering (IWICImagingFactory::CreateBitmap
and ID2D1Factory::CreateWicBitmapRenderTarget
) and then copy it to a hardware bitmap back on the thread with the hardware render target via ID2D1RenderTarget::CreateBitmapFromWicBitmap
. And then blit that using ID2D1RenderTarget::DrawBitmap
.
This is how paint.net 4.0 does selection rendering. When you're drawing a selection with the lasso tool, it will use a background thread to draw the selection outline asynchronously (the UI thread does not wait for this to complete). You can end up with a very complicated polygon due to the stroke style and animations. I render it 4 times, where each animation frame has a slightly different offset for the dashed stroke style.
Obviously this rendering can take awhile as the polygon becomes more complex (that is, if you keep scribbling for awhile). I have a few other special optimizations for when you use the Move Selection tool which allows you to do transformations (rotate, translate, scale): if the background thread hasn't yet re-rendered the current polygon with the new transform, then I will render the old bitmap (with the current polygon and old transform) with the new transform applied. The selection outline may be distorted (scaling) or clipped (translated outside of viewable area) while the background thread catches up, but it's a small price to pay for 60fps responsiveness. This optimization works very well because you can't be modifying the polygon and transform of a selection at the same time.
Ok, I've found a solution.
Basically, all I needed is to modify approach 2 to use DXGI resource sharing between two DirectX factory sets. I'll skip all the gory details (they can be found here: http://xboxforums.create.msdn.com/forums/t/66208.aspx), but basic steps are:
ID3D11Device2
from main resource set, create D3D 2D texture by CreateTexture2D
D3D11_BIND_RENDER_TARGET
, D3D11_BIND_SHADER_RESOURCE
, D3D11_RESOURCE_MISC_SHARED_NTHANDLE
and D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX
flags.IDXGIResource1
and calling CreateSharedHandle
from it with XGI_SHARED_RESOURCE_READ
and DXGI_SHARED_RESOURCE_WRITE
.ID3D11Device2::OpenSharedResource1
.IDXGIKeyedMutex::AcquireSync
), create render target from it (ID2D1Factory2::CreateDxgiSurfaceRenderTarget
), draw on it and release mutex (IDXGIKeyedMutex::ReleaseSync
).Note that mutex locking stuff is necessary. Not doing it results in some cryptic DirectX debug error messages, and erroneous operation or even crashing.