Yes, you can do it.
In your header keep:
ID2D1Device* m_d2device;
ID2D1DeviceContext* m_deviceContext;
ID2D1RenderTarget* m_renderTargetView;
ID2D1Factory* m_factory2d;
IDXGISurface* m_dxgiBackbuffer;
IDWriteFactory1* m_writeFactory;
Factory 2D:
HRESULT result;
// create the D2D factory
D2D1_FACTORY_OPTIONS options2d;
options2d.debugLevel = D2D1_DEBUG_LEVEL_NONE;
result = D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_MULTI_THREADED, options2d, &m_factory2d);
Create DXGI surface:
// set up the D2D render target using the back buffer
m_swapChain->GetBuffer(0, IID_PPV_ARGS(&m_dxgiBackbuffer));
D2D1_RENDER_TARGET_PROPERTIES props =
D2D1::RenderTargetProperties
(D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat
(
DXGI_FORMAT_UNKNOWN,
D2D1_ALPHA_MODE_PREMULTIPLIED
)
);
m_factory2d->CreateDxgiSurfaceRenderTarget(
m_dxgiBackbuffer,
props,
&m_d2dRenderTarget);
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(m_writeFactory),
(IUnknown**)(&m_writeFactory));
In your render loop first render 3D things then 2D things. You can both render 2D over 3D or vice versa. Just pass it trough 3D. I tested both on DX11.0 and win10. I highly advise you to seperate 3D call and 2D call into different classes that would provide better management.
IDXGISurfacewithIDirect3DSurface? :docs.microsoft.com/en-us/windows/desktop/api/DXGI/… - Muzib