The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

This documentation is archived and is not being maintained.

Direct2D and Direct3D Interoperability Overview

Hardware accelerated 2-D and 3-D graphics are increasingly becoming a part of non-gaming applications, and most gaming applications use 2-D graphics in the form of menus and Heads-Up Displays (HUDs). There is lots of value that can be added by enabling traditional 2-D rendering to be mixed with Direct3D rendering in an efficient manner.

This topic describes how to integrate 2-D and 3-D graphics by using Direct2D and Direct3D.

Interoperability Through DXGI

As of Direct3D 10, the Direct3D runtime uses DXGI for resource management. The DXGI runtime layer provides cross-process sharing of video memory surfaces and serves as the foundation for other video memory-based runtime platforms. Direct2D uses DXGI to interoperate with Direct3D.

There are two primary ways to use Direct2D and Direct3D together:

You can write Direct2D content to a Direct3D surface by obtaining an IDXGISurface and using it with the CreateDxgiSurfaceRenderTarget to create an ID2D1RenderTarget. You can then use the render target to add a two-dimensional interface or background to three-dimensional graphics, or use a Direct2D drawing as a texture for a three dimensional object.

Writing to a Direct3D Surface with a DXGI Surface Render Target

To write to a Direct3D surface, you obtain an IDXGISurface and pass it to the CreateDxgiSurfaceRenderTarget method to create a DXGI surface render target. You can then use the DXGI surface render target to draw 2-D content to the DXGI surface.

A DXGI surface render target is a kind of ID2D1RenderTarget. Like other Direct2D render targets, you can use it to create resources and issue drawing commands.

The DXGI surface render target and the DXGI surface must use the same DXGI format. If you specify the DXGI_FORMAT_UNKOWN format when you create the render target, it will automatically use the surface's format.

Creating a DXGI Surface

With Direct3D 10, there are several ways to obtain a DXGI surface. You can create an IDXGISwapChain for a device, then use the swap chain's GetBuffer method to obtain a DXGI surface. Or, you can use a device to create a texture, then use that texture as a DXGI surface.

Using Direct2D Content as a Texture

Another way to use Direct2D content with Direct3D is to use Direct2D to generate a 2-D texture and then apply that texture to a 3-D model. You do this by creating an ID3D10Texture2D, obtaining a DXGI surface from the texture, and then using the surface to create a DXGI surface render target. The ID3D10Texture2D surface must use the D3D10_BIND_RENDER_TARGET bind flag and use a DXGI format supported by DXGI surface render targets. For a list of supported DXGI formats, see Supported Pixel Formats and Alpha Modes.

Example: Use Direct2D Content as a Texture

The following examples show how to create a DXGI surface render target that renders to a 2-D texture (represented by an ID3D10Texture2D).

if (SUCCEEDED(hr))
{
// Create a D2D render target which can draw into our offscreen D3D// surface. Given that we use a constant size for the texture, we// fix the DPI at 96.
D2D1_RENDER_TARGET_PROPERTIES props =
D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
96,
96
);

Now that you have obtained a Direct2D render target and associated it with a Direct3D texture, you can use the render target to draw Direct2D content to that texture, and you can apply that texture to Direct3D primitives.

Code is omitted from this sample.

Resizing a DXGI Surface Render Target

DXGI surface render targets do not support the ID2D1RenderTarget::Resize method. To resize a DXGI surface render target, the application must release and re-create it.

This operation can potentially create performance issues. The render target might be the last active Direct2D resource that keeps a reference to the ID3D10Device1 associated with the render target's DXGI surface. If the application releases the render target and the ID3D10Device1 reference is destroyed, a new one must be recreated.

You can avoid this potentially expensive operation by keeping at least one Direct2D resource that was created by the render target while you re-create that render target. The following are some Direct2D resources that work for this approach:

To accommodate this approach, your resize method should test to see whether the Direct3D device is available. If it is available, release and re-create your DXGI surface render targets, but keep all the resources that they created previously and reuse them. This works because, as described in the Resources Overview, resources created by two render targets are compatible when both render targets are associated with the same Direct3D device.