I heard all over the internet that Direct2D is only usable with a Direct3D 10.1 device. But I decided to experiment with D2D to see if that was really true. Once I finished all my experimenting... I came to the doubtfull conclusion that Direct2d can actually use a direct3d 11 device.
Anyway, here is my code that i used to experiment.. I tried to create a DXGI surface(that points to a D3D11 2D texture) with a direct3d 11 device.
/* SharedTextureDesc object contains description of a 2D texture*/
D3D11device->CreateTexture2D(&SharedTextureDesc, NULL, &SharedTexture);
SharedTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&SharedResource);
SharedResource->GetSharedHandle(&SharedHandle);
D3D11device->OpenSharedResource(SharedHandle, __uuidof(IDXGISurface1), (void**)&SharedSurface);
The code above just shows that I created a DXGI surface with the direct3d 11 device. Now below, I am using the DXGI surface to create the render target for Direct2D.
/* RenderTargetProperties contains the description for render target */
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), (void**) &D2DFactory);
D2DFactory->CreateDxgiSurfaceRenderTarget(SharedSurface, &RenderTargetProperties, &RenderTargetD2D)
The render target for Direct2d seems to work, I can use it to actually render text onto my game. The method CreateDxgiSurfaceRenderTarget()
even returned a S_OK
, which tells me it succeeded.
But my only question is... Can the render target for direct2d still work without errors? or can Direct2D not work with a Direct3D 11 device at all?
Direct2D on a DirectX 11.1 system can work with a Direct3D 11 device. DirectX 11.1 is included with Windows 8 or later, and is partially supported on Windows 7 SP1 via KB2670838
With a DirectX 11.0 system (i.e. Windows Vista SP2 with KB971644, Windows 7 RTM, or Windows 7 SP1 without KB2670838 installed), you have to use DXGI surface sharing so that Direct2D works on a Direct3D 10.1 device and you 'share' the result with your Direct3D 11 device.
See MSDN, DirectX 11.1 and Windows 7, and DirectX 11.1 and Windows 7 Update.