ID2D1Factory::CreateDxgiSurfaceRenderTarget(IDXGISurface*,constD2D1_RENDER_TARGET_PROPERTIES*,ID2D1RenderTarget**) method (d2d1.h)

Creates a render target that draws to a DirectX Graphics Infrastructure (DXGI) surface.

Syntax

HRESULT CreateDxgiSurfaceRenderTarget(
  [in]  IDXGISurface                        *dxgiSurface,
  [in]  const D2D1_RENDER_TARGET_PROPERTIES *renderTargetProperties,
  [out] ID2D1RenderTarget                   **renderTarget
);

Parameters

[in] dxgiSurface

Type: IDXGISurface*

The IDXGISurface to which the render target will draw.

[in] renderTargetProperties

Type: const D2D1_RENDER_TARGET_PROPERTIES*

The rendering mode, pixel format, remoting options, DPI information, and the minimum DirectX support required for hardware rendering. For information about supported pixel formats, see Supported Pixel Formats and Alpha Modes.

[out] renderTarget

Type: ID2D1RenderTarget**

When this method returns, contains the address of the pointer to the ID2D1RenderTarget object created by this method.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

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 type 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.

The DXGI surface render target does not perform DXGI surface synchronization.

For more information about creating and using DXGI surface render targets, see the Direct2D and Direct3D Interoperability Overview.

To work with Direct2D, the Direct3D device that provides the IDXGISurface must be created with the D3D10_CREATE_DEVICE_BGRA_SUPPORT flag.

When you create a render target and hardware acceleration is available, you allocate resources on the computer's GPU. By creating a render target once and retaining it as long as possible, you gain performance benefits. Your application should create render targets once and hold onto them for the life of the application or until the render target's EndDraw method returns the D2DERR_RECREATE_TARGET error. When you receive this error, you need to recreate the render target (and any resources it created).

Examples

The following example obtains a DXGI surface (pBackBuffer) from an IDXGISwapChain and uses it to create a DXGI surface render target.

// Initialize *hwnd* with the handle of the window displaying the rendered content.
HWND hwnd;

// Get a surface in the swap chain
hr = m_pSwapChain->GetBuffer(
    0,
    IID_PPV_ARGS(&pBackBuffer));

if (SUCCEEDED(hr))
{
    // Create the DXGI Surface Render Target.
    float dpi = GetDpiForWindow(hwnd);

    D2D1_RENDER_TARGET_PROPERTIES props =
        D2D1::RenderTargetProperties(
            D2D1_RENDER_TARGET_TYPE_DEFAULT,
            D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
            dpi,
            dpi
            );

    // Create a Direct2D render target that can draw into the surface in the swap chain.

    hr = m_pD2DFactory->CreateDxgiSurfaceRenderTarget(
        pBackBuffer,
        &props,
        &m_pBackBufferRT);
}

Requirements

Requirement Value
Target Platform Windows
Header d2d1.h
Library D2d1.lib
DLL D2d1.dll

See also

CreateDxgiSurfaceRenderTarget(IDXGISurface,const D2D1_RENDER_TARGET_PROPERTIES &,ID2D1RenderTarget)

Direct2D and Direct3D interoperability overview

ID2D1Factory