Klaffy
Klaffy

Reputation: 123

Rendering Directx 11 Back Buffer onto Direct2d

I want to make a simple level editor, in which editor's gui will be rendered by Direct2d and all the other 3D graphics will be handle by Directx 11.. Like Unity3D does.. as shown in the figure...

https://i.sstatic.net/tf5G1.png

As you can see the 3D graphics are being render inside the GUI.. how can i share backbuffer of my directx 11 application with a direct2d(use to render gui)... so that 3D scene will be rendered inside the GUI.. or please suggest some other suggest other ideas other then using direct2d... also considering the performance.. Thank you..

Upvotes: 1

Views: 1428

Answers (2)

Gomi Odabaşıoğlu
Gomi Odabaşıoğlu

Reputation: 176

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.

Upvotes: 0

bunglehead
bunglehead

Reputation: 1179

I don't know if sharing is possible. It's probably easier to create another d3d11 texture, get surface from it, then create Direct2D target/context on this surface. Later you can render this UI texture together with your 3D content, like you would do for any other texture.

Upvotes: 0

Related Questions