D2D using D3D11on12 - Win32 apps (2024)

  • Article

The D3D1211on12 sample demonstrates how to render D2D content over D3D12 content by sharing resources between an 11 based device and a 12 based device.

  • Create an ID3D11On12Device
  • Create a D2D factory
  • Create a render target for D2D
  • Create basic D2D text objects
  • Updating the main render loop
  • Run the sample
  • Related topics

Create an ID3D11On12Device

The first step is to create an ID3D11On12Device after the ID3D12Device has been created, which involves creating an ID3D11Device that is wrapped around the ID3D12Device via the API D3D11On12CreateDevice. This API also takes in, among other parameters, an ID3D12CommandQueue so that the 11On12 device can submit its commands. After the ID3D11Device is created, you can query the ID3D11On12Device interface from it. This is the primary device object that will be used to set up D2D.

In the LoadPipeline method, setup the devices.

 // Create an 11 device wrapped around the 12 device and share // 12's command queue. ComPtr<ID3D11Device> d3d11Device; ThrowIfFailed(D3D11On12CreateDevice( m_d3d12Device.Get(), d3d11DeviceFlags, nullptr, 0, reinterpret_cast<IUnknown**>(m_commandQueue.GetAddressOf()), 1, 0, &d3d11Device, &m_d3d11DeviceContext, nullptr )); // Query the 11On12 device from the 11 device. ThrowIfFailed(d3d11Device.As(&m_d3d11On12Device));
Call flowParameters
ID3D11Device
D3D11On12CreateDevice

Create a D2D factory

Now that we have an 11On12 device, we use it to create a D2D factory and device just as it would normally be done with D3D11.

Add to the LoadAssets method.

 // Create D2D/DWrite components. { D2D1_DEVICE_CONTEXT_OPTIONS deviceOptions = D2D1_DEVICE_CONTEXT_OPTIONS_NONE; ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory3), &d2dFactoryOptions, &m_d2dFactory)); ComPtr<IDXGIDevice> dxgiDevice; ThrowIfFailed(m_d3d11On12Device.As(&dxgiDevice)); ThrowIfFailed(m_d2dFactory->CreateDevice(dxgiDevice.Get(), &m_d2dDevice)); ThrowIfFailed(m_d2dDevice->CreateDeviceContext(deviceOptions, &m_d2dDeviceContext)); ThrowIfFailed(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &m_dWriteFactory)); }
Call flowParameters
D2D1_DEVICE_CONTEXT_OPTIONS
D2D1CreateFactoryD2D1_FACTORY_TYPE
IDXGIDevice
ID2D1Factory3::CreateDevice
ID2D1Device::CreateDeviceContext
DWriteCreateFactoryDWRITE_FACTORY_TYPE

Create a render target for D2D

D3D12 owns the swap chain, so if we want to render to the back buffer using our 11On12 device (D2D content), then we need to create wrapped resources of type ID3D11Resource from the back buffers of type ID3D12Resource. This links the ID3D12Resource with a D3D11 based interface so that it can be used with the 11On12 device. After we have a wrapped resource, we can then create a render target surface for D2D to render to, also in the LoadAssets method.

// Initialize *hwnd* with the handle of the window displaying the rendered content.HWND hwnd;// Query the window's dpi settings, which will be used to create// D2D's render targets.float dpi = GetDpiForWindow(hwnd);D2D1_BITMAP_PROPERTIES1 bitmapProperties = D2D1::BitmapProperties1( D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED), dpi, dpi); // Create frame resources.{ CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart()); // Create a RTV, D2D render target, and a command allocator for each frame. for (UINT n = 0; n < FrameCount; n++) { ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n]))); m_d3d12Device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle); // Create a wrapped 11On12 resource of this back buffer. Since we are // rendering all D3D12 content first and then all D2D content, we specify // the In resource state as RENDER_TARGET - because D3D12 will have last // used it in this state - and the Out resource state as PRESENT. When // ReleaseWrappedResources() is called on the 11On12 device, the resource // will be transitioned to the PRESENT state. D3D11_RESOURCE_FLAGS d3d11Flags = { D3D11_BIND_RENDER_TARGET }; ThrowIfFailed(m_d3d11On12Device->CreateWrappedResource( m_renderTargets[n].Get(), &d3d11Flags, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT, IID_PPV_ARGS(&m_wrappedBackBuffers[n]) )); // Create a render target for D2D to draw directly to this back buffer. ComPtr<IDXGISurface> surface; ThrowIfFailed(m_wrappedBackBuffers[n].As(&surface)); ThrowIfFailed(m_d2dDeviceContext->CreateBitmapFromDxgiSurface( surface.Get(), &bitmapProperties, &m_d2dRenderTargets[n] )); rtvHandle.Offset(1, m_rtvDescriptorSize); ThrowIfFailed(m_d3d12Device->CreateCommandAllocator( D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocators[n]))); }}
Call flowParameters
GetDpiForWindowA window handle
D2D1_BITMAP_PROPERTIES1
BitmapProperties1
[D2D1_BITMAP_OPTIONS](/windows/desktop/api/d2d1_1/ne-d2d1_1-d2d1_bitmap_options)
[PixelFormat](/windows/desktop/api/d2d1helper/nf-d2d1helper-pixelformat)
[DXGI_FORMAT](/windows/desktop/api/dxgiformat/ne-dxgiformat-dxgi_format)
[D2D1_ALPHA_MODE](/windows/desktop/api/dcommon/ne-dcommon-d2d1_alpha_mode)
CD3DX12_CPU_DESCRIPTOR_HANDLEGetCPUDescriptorHandleForHeapStart
IDXGISwapChain::GetBuffer
CreateRenderTargetView
D3D11_RESOURCE_FLAGSD3D11_BIND_FLAG
CreateWrappedResourceD3D12_RESOURCE_STATES
IDXGISurface
ID2D1DeviceContext::CreateBitmapFromDxgiSurface
CreateCommandAllocatorD3D12_COMMAND_LIST_TYPE

Create basic D2D text objects

Now we have an ID3D12Device to render 3D content, an ID2D1Device that is shared with our 12 device via an ID3D11On12Device - which we can use to render 2D content - and they are both configured to render to the same swap chain. This sample simply uses the D2D device to render text over the 3D scene, similar to how games render their UI. For that, we need to create some basic D2D objects, still in the LoadAssets method.

 // Create D2D/DWrite objects for rendering text. { ThrowIfFailed(m_d2dDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &m_textBrush)); ThrowIfFailed(m_dWriteFactory->CreateTextFormat( L"Verdana", NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 50, L"en-us", &m_textFormat )); ThrowIfFailed(m_textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER)); ThrowIfFailed(m_textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER)); }
Call flowParameters
ID2D1RenderTarget::CreateSolidColorBrushColorF
IDWriteFactory::CreateTextFormatDWRITE_FONT_WEIGHT
IDWriteTextFormat::SetTextAlignmentDWRITE_TEXT_ALIGNMENT
IDWriteTextFormat::SetParagraphAlignmentDWRITE_PARAGRAPH_ALIGNMENT

Updating the main render loop

Now that the initialization of the sample is complete, we can move on to the main render loop.

// Render the scene.void D3D1211on12::OnRender(){ // Record all the commands we need to render the scene into the command list. PopulateCommandList(); // Execute the command list. ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() }; m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists); RenderUI(); // Present the frame. ThrowIfFailed(m_swapChain->Present(0, 0)); MoveToNextFrame();}
Call flowParameters
ID3D12CommandList
ExecuteCommandLists
IDXGISwapChain1::Present1

The only thing new to our render loop is the RenderUI call, which will use D2D to render our UI. Notice that we execute all of our D3D12 command lists first to render our 3D scene, and then we render our UI on top of that. Before we dive into RenderUI, we must look at a change to PopulateCommandLists. In other samples we commonly put a resource barrier on the command list prior to closing it to transition the back buffer from the render target state to the present state. However, in this sample we remove that resource barrier, because we still need to render to the back buffers with D2D. Note that when we created our wrapped resources of the back buffer that we specified the render target state as the “IN” state and the present state as the “OUT” state.

RenderUI is fairly straight-forward in terms of D2D usage. We set our render target and render our text. However, before using any wrapped resources on an 11On12 device, such as our back buffer render targets, we must call the AcquireWrappedResources API on the 11On12 device. After rendering we call the ReleaseWrappedResources API on the 11On12 device. By calling ReleaseWrappedResources we incur a resource barrier behind the scenes that will transition the specified resource to the “OUT” state specified at creation time. In our case, this is the present state. Finally, in order to submit all of our commands performed on the 11On12 device to the shared ID3D12CommandQueue, we must call Flush on the ID3D11DeviceContext.

// Render text over D3D12 using D2D via the 11On12 device.void D3D1211on12::RenderUI(){ D2D1_SIZE_F rtSize = m_d2dRenderTargets[m_frameIndex]->GetSize(); D2D1_RECT_F textRect = D2D1::RectF(0, 0, rtSize.width, rtSize.height); static const WCHAR text[] = L"11On12"; // Acquire our wrapped render target resource for the current back buffer. m_d3d11On12Device->AcquireWrappedResources(m_wrappedBackBuffers[m_frameIndex].GetAddressOf(), 1); // Render text directly to the back buffer. m_d2dDeviceContext->SetTarget(m_d2dRenderTargets[m_frameIndex].Get()); m_d2dDeviceContext->BeginDraw(); m_d2dDeviceContext->SetTransform(D2D1::Matrix3x2F::Identity()); m_d2dDeviceContext->DrawTextW( text, _countof(text) - 1, m_textFormat.Get(), &textRect, m_textBrush.Get() ); ThrowIfFailed(m_d2dDeviceContext->EndDraw()); // Release our wrapped render target resource. Releasing // transitions the back buffer resource to the state specified // as the OutState when the wrapped resource was created. m_d3d11On12Device->ReleaseWrappedResources(m_wrappedBackBuffers[m_frameIndex].GetAddressOf(), 1); // Flush to submit the 11 command list to the shared command queue. m_d3d11DeviceContext->Flush();}
Call flowParameters
D2D1_SIZE_F
D2D1_RECT_FRectF
AcquireWrappedResources
ID2D1DeviceContext::SetTarget
ID2D1RenderTarget::BeginDraw
ID2D1RenderTarget::SetTransformMatrix3x2F
ID2D1RenderTarget::DrawTextW
ID2D1RenderTarget::EndDraw
ReleaseWrappedResources
ID3D11DeviceContext::Flush

Run the sample

D2D using D3D11on12 - Win32 apps (1)

D3D12 Code Walk-Throughs

Direct3D 11 on 12

Direct3D 12 Interop

11on12 Reference

D2D using D3D11on12 - Win32 apps (2024)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Dong Thiel

Last Updated:

Views: 6103

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.