Nota
O acesso a esta página requer autorização. Podes tentar iniciar sessão ou mudar de diretório.
O acesso a esta página requer autorização. Podes tentar mudar de diretório.
In this topic you will learn about creating Direct2Ddevice context in Windows 8. Estas informações aplicam-se se estiver a desenvolver aplicações da Loja Windows ou uma aplicação de ambiente de trabalho utilizando Direct2D. Este tópico descreve a finalidade dos objetos de contexto de dispositivo Direct2D, como criar esse objeto e um guia passo a passo sobre como renderizar e exibir primitivos e imagens Direct2D. Você também aprenderá sobre como alternar destinos de renderização e adicionar efeitos ao seu aplicativo.
- O que é um dispositivo Direct2D?
- O que é um contexto de dispositivo Direct2D?
- Rendering with Direct2D on Windows 8
- Por que usar um contexto de dispositivo para renderizar?
- Como criar um contexto de dispositivo Direct2D para renderizar
- Selecionar um alvo
- Como renderizar e exibir
O que é um dispositivo Direct2D?
Você precisa de um dispositivo Direct2D e um dispositivo Direct3D para criar um contexto de dispositivo Direct2D. A Direct2D device (exposes an ID2D1Device interface pointer) represents a display adapter. A Direct3D device (exposes an ID3D11Device interface pointer) is associated with a Direct2D device. Cada aplicativo deve ter um dispositivo Direct2D , mas pode ter mais de um dispositivo .
O que é um contexto de dispositivo Direct2D?
A Direct2Ddevice context (exposes an ID2D1DeviceContext interface pointer) represents a set of state and command buffers that you use to render to a target. Você pode chamar métodos no contexto do dispositivo para definir o estado do pipeline e gerar comandos de renderização usando os recursos de propriedade de um dispositivo.
Renderização com Direct2D no Windows 8
On Windows 7 and earlier, you use a ID2D1HwndRenderTarget or another render target interface to render to a window or surface. A partir do Windows 8, não recomendamos a renderização usando métodos que dependem de interfaces como ID2D1HwndRenderTarget porque eles não funcionarão com aplicativos da Windows Store. You can use a device context to render to an Hwnd if you want to make a desktop app and still take advantage of the device context's additional features. However, the device context is required to render content in a Windows Store apps with Direct2D.
Por que usar um contexto de dispositivo para renderizar?
- Você pode renderizar para aplicativos da Windows Store.
- Você pode alterar o destino de renderização a qualquer momento antes, durante e depois da renderização. The device context ensures that the calls to drawing methods are executed in order and applies them when you switch the render target.
- Você pode usar mais de um tipo de janela com um contexto de dispositivo. You can use a device context and a DXGI swap chain to render directly to a Windows::UI::Core::CoreWindow or a Windows::UI::XAML::SwapChainBackgroundPanel.
- Pode utilizar o contexto de dispositivo Direct2D para criar efeitos Direct2D e para renderizar a saída de um efeito de imagem ou de um gráfico de efeitos para um alvo de renderização.
- You can have multiple device contexts, which can be helpful for improving performance in a threaded app. See Multithreaded Direct2D apps for more information.
- O contexto do dispositivo interopera estreitamente com o Direct3D, dando-lhe mais acesso às opções do Direct3D.
Como criar um contexto de dispositivo Direct2D para renderização
O código aqui mostra como criar um dispositivo Direct3D11, obter o dispositivo DXGI associado, criar um dispositivo Direct2D e, finalmente, criar o contexto do dispositivo Direct2D para renderização.
Aqui está um diagrama das chamadas de método e as interfaces que esse código usa.
Observação
Este código pressupõe que você já tenha um objetoID2D1Factory1, para obter mais informações, consulte a página de referência ID2D1Factory .
// This flag adds support for surfaces with a different color channel ordering than the API default.
// You need it for compatibility with Direct2D.
UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
// This array defines the set of DirectX hardware feature levels this app supports.
// The ordering is important and you should preserve it.
// Don't forget to declare your app's minimum required feature level in its
// description. All apps are assumed to support 9.1 unless otherwise stated.
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
// Create the DX11 API device object, and get a corresponding context.
ComPtr<ID3D11Device> device;
ComPtr<ID3D11DeviceContext> context;
DX::ThrowIfFailed(
D3D11CreateDevice(
nullptr, // specify null to use the default adapter
D3D_DRIVER_TYPE_HARDWARE,
0,
creationFlags, // optionally set debug and Direct2D compatibility flags
featureLevels, // list of feature levels this app can support
ARRAYSIZE(featureLevels), // number of possible feature levels
D3D11_SDK_VERSION,
&device, // returns the Direct3D device created
&m_featureLevel, // returns feature level of device created
&context // returns the device immediate context
)
);
ComPtr<IDXGIDevice> dxgiDevice;
// Obtain the underlying DXGI device of the Direct3D11 device.
DX::ThrowIfFailed(
device.As(&dxgiDevice)
);
// Obtain the Direct2D device for 2-D rendering.
DX::ThrowIfFailed(
m_d2dFactory->CreateDevice(dxgiDevice.Get(), &m_d2dDevice)
);
// Get Direct2D device's corresponding device context object.
DX::ThrowIfFailed(
m_d2dDevice->CreateDeviceContext(
D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
&m_d2dContext
)
);
Vamos percorrer as etapas no exemplo de código anterior.
Get an ID3D11Device interface pointer you will need this to create the device context.
Declare the creation flags to set up the Direct3D device for BGRA support. Direct2D requer a ordem de cores BGRA.
Declare uma matriz de entradas de D3D_FEATURE_LEVEL que representam o conjunto de níveis de recursos suportados pelo seu aplicativo.
Observação
Direct3D pesquisa sua lista até encontrar o nível de recurso suportado pelo sistema host.
Use a função D3D11CreateDevice para criar um ID3D11Device objeto, a função também retornará um ID3D11DeviceContext objeto, mas esse objeto não é necessário para este exemplo.
Query the Direct3D 11 device for its DXGI Device interface.
Crie um ID2D1Device objeto chamando o ID2D1Factory::CreateDevice método e passando o IDXGIDevice objeto.
Create an ID2D1DeviceContext pointer using the ID2D1Device::CreateDeviceContext method.
Seleção de um alvo
The code here shows you how to get the 2 dimensional Direct3D texture for the window back buffer and create a bitmap target that links to this texture to which the Direct2D device context renders.
// Allocate a descriptor.
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {0};
swapChainDesc.Width = 0; // use automatic sizing
swapChainDesc.Height = 0;
swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format
swapChainDesc.Stereo = false;
swapChainDesc.SampleDesc.Count = 1; // don't use multi-sampling
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 2; // use double buffering to enable flip
swapChainDesc.Scaling = DXGI_SCALING_NONE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect
swapChainDesc.Flags = 0;
// Identify the physical adapter (GPU or card) this device is runs on.
ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(
dxgiDevice->GetAdapter(&dxgiAdapter)
);
// Get the factory object that created the DXGI device.
ComPtr<IDXGIFactory2> dxgiFactory;
DX::ThrowIfFailed(
dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory))
);
// Get the final swap chain for this window from the DXGI factory.
DX::ThrowIfFailed(
dxgiFactory->CreateSwapChainForCoreWindow(
device.Get(),
reinterpret_cast<IUnknown*>(m_window),
&swapChainDesc,
nullptr, // allow on all displays
&m_swapChain
)
);
// Ensure that DXGI doesn't queue more than one frame at a time.
DX::ThrowIfFailed(
dxgiDevice->SetMaximumFrameLatency(1)
);
// Get the backbuffer for this window which is be the final 3D render target.
ComPtr<ID3D11Texture2D> backBuffer;
DX::ThrowIfFailed(
m_swapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer))
);
// Now we set up the Direct2D render target bitmap linked to the swapchain.
// Whenever we render to this bitmap, it is directly rendered to the
// swap chain associated with the window.
D2D1_BITMAP_PROPERTIES1 bitmapProperties =
BitmapProperties1(
D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
m_dpi,
m_dpi
);
// Direct2D needs the dxgi version of the backbuffer surface pointer.
ComPtr<IDXGISurface> dxgiBackBuffer;
DX::ThrowIfFailed(
m_swapChain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer))
);
// Get a D2D surface from the DXGI back buffer to use as the D2D render target.
DX::ThrowIfFailed(
m_d2dContext->CreateBitmapFromDxgiSurface(
dxgiBackBuffer.Get(),
&bitmapProperties,
&m_d2dTargetBitmap
)
);
// Now we can set the Direct2D render target.
m_d2dContext->SetTarget(m_d2dTargetBitmap.Get());
Vamos percorrer as etapas no exemplo de código anterior.
Allocate a DXGI_SWAP_CHAIN_DESC1 structure and define the settings for the swap chain.
Essas configurações mostram um exemplo de como criar uma cadeia de permuta que um aplicativo da Windows Store pode usar.
Obtenha o adaptador no qual o dispositivo Direct3D e o dispositivo DXGI estão a ser executados e obtenha o objeto IDXGIFactory associado a eles. You must use this DXGI factory to ensure the swap chain is created on the same adapter.
Chame o IDXGIFactory2::CreateSwapChainForCoreWindow método para criar a cadeia de permuta. Use the Windows::UI::CoreWindow class for the main window of a Windows Store app.
Certifique-se de definir a latência máxima do quadro como 1 para minimizar o consumo de energia.
Se tu quiseres renderizar conteúdo Direct2D numa aplicação da Windows Store, consulta o método CreateSwapChainForComposition.
Get the back buffer from the swap chain. The back buffer exposes an ID3D11Texture2D interface allocated by the swap chain
Declare a D2D1_BITMAP_PROPERTIES1 struct and set the property values. Defina o formato de pixel como BGRA porque este é o formato que o dispositivo Direct3D e o dispositivo DXGI usar.
Get the back buffer as an IDXGISurface to pass to Direct2D. Direct2D doesn't accept an ID3D11Texture2D directly.
Crie um objeto ID2D1Bitmap usando o método ID2D1DeviceContext::CreateBitmapFromDxgiSurface a partir do buffer traseiro.
Now the Direct2D bitmap is linked to the back buffer. Set the target on the Direct2D device context to the bitmap.
Como renderizar e exibir
Now that you have a target bitmap, you can draw primitives, images, image effects, and text to it using the Direct2D device context. O código aqui mostra como desenhar um retângulo.
ComPtr<ID2D1SolidColorBrush> pBlackBrush;
DX::ThrowIfFailed(
m_d2dContext->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&pBlackBrush
)
);
m_d2dContext->BeginDraw();
m_d2dContext->DrawRectangle(
D2D1::RectF(
rc.left + 100.0f,
rc.top + 100.0f,
rc.right - 100.0f,
rc.bottom - 100.0f),
pBlackBrush);
DX::ThrowIfFailed(
m_d2dContext->EndDraw()
);
DX::ThrowIfFailed(
m_swapChain->Present1(1, 0, ¶meters);
);
Vamos percorrer as etapas no exemplo de código anterior.
- Chame o CreateSolidColorBrush para criar um pincel para pintar o retângulo.
- Chame o método BeginDraw antes de emitir qualquer comando de desenho.
- Call the DrawRectangle method the rectangle to be drawn and the brush.
- Call the EndDraw method after you've finished issuing drawing commands.
- Exiba o resultado chamando o método IDXGISwapChain::Present.
Agora você pode usar o contexto do dispositivo Direct2D desenhar primitivos, imagens, efeitos de imagem e texto na tela.