2

我正在尝试根据 Windows 教程编写 Direct2D 桌面应用程序,但在创建 SwapChain1 时遇到问题。在下面的代码中,一切都被初始化,直到 CreateSwapChainForHwnd。指针 m_pDXGISwapChain1 保持为 NULL。除 pOutput 之外的所有指针都是 ComPtrs。

D2D1_FACTORY_OPTIONS options;
ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));

HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
                                __uuidof(ID2D1Factory1), &options, &m_pD2DFactory1);
if(SUCCEEDED(hr))
{
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
    D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0 };
    hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, 0, creationFlags,     
        featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, &m_pD3DDevice, 
        &m_featureLevel, &m_pD3DDeviceContext);
}

if(SUCCEEDED(hr))
    hr = m_pD3DDevice.As(&m_pDXGIDevice1);

if(SUCCEEDED(hr))
    hr = m_pD2DFactory1->CreateDevice(m_pDXGIDevice1.Get(), &m_pD2DDevice);

if(SUCCEEDED(hr))
    hr =  m_pD2DDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &m_pD2DDeviceContext);

 if(SUCCEEDED(hr))
    hr = m_pDXGIDevice1->GetAdapter(&m_pDXGIAdapter); 

 if(SUCCEEDED(hr))
    hr = m_pDXGIAdapter->GetParent(IID_PPV_ARGS(&m_pDXGIFactory2));

DXGI_SWAP_CHAIN_DESC1 swapChainDesc1 = {0};
swapChainDesc1.Width = 0; 
swapChainDesc1.Height = 0;
swapChainDesc1.Format = DXGI_FORMAT_B8G8R8A8_UNORM;      
swapChainDesc1.Stereo = false; 
swapChainDesc1.SampleDesc.Count = 1;   
swapChainDesc1.SampleDesc.Quality = 0;
swapChainDesc1.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc1.BufferCount = 2;   
swapChainDesc1.Scaling = DXGI_SCALING_NONE;
swapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
swapChainDesc1.AlphaMode = DXGI_ALPHA_MODE_IGNORE;
swapChainDesc1.Flags = 0;

IDXGIOutput *pOutput;
m_pDXGIAdapter->EnumOutputs(0, &pOutput);

if(SUCCEEDED(hr))
   hr = m_pDXGIFactory2->CreateSwapChainForHwnd(
            static_cast<IUnknown*>(m_pD3DDevice.Get()), m_hwnd, &swapChainDesc1, 
            NULL, pOutput, &m_pDXGISwapChain1);

if(SUCCEEDED(hr))
    hr = m_pDXGIDevice1->SetMaximumFrameLatency(1);

if(SUCCEEDED(hr))
    hr = m_pDXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&m_pDXGIBackBuffer));
4

2 回答 2

1

如果您的所有指针都是ComPtr,那么调用应该如下所示:

ComPtr<ID3D11Device> d3dDevice;
ComPtr<IDXGIFactory2> dxgiFactory;

// 假设 d3dDevice 和 dxgiFactory 已正确初始化:

ComPtr<IDXGISwapChain1> swapChain;
dxgiFactory->CreateSwapChainForHwnd(d3dDevice.Get(), hWnd, &swapChainDescription, nullptr, nullptr, swapChain.GetAddressOf())

至于您的交换链描述,如果您正在制作非 Windows 应用商店应用程序,则应设置

swapChainDescription.Scaling = DXGI_SCALING_STRETCH;
swapChainDescription.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

这两个值都是0,因此您可以将它们排除在外。

这是我使用的完整交换链描述:

DXGI_SWAP_CHAIN_DESC1 swapChainDescription = {};
swapChainDescription.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swapChainDescription.SampleDesc.Count = 1;
swapChainDescription.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDescription.BufferCount = 2;

有关如何正确设置 Direct2D 1.1 的完整演练,请参阅本文 - 包括 CreateSwapChainForHwnd 调用:http: //msdn.microsoft.com/en-us/magazine/dn198239.aspx

于 2014-02-11T20:22:10.833 回答
-1

Direct2D 在用户级别没有 SwapChain,SwapChain 用于 Direct3D,我在您的帖子中看到了一些 DirectX 11 代码,您真的想要 Direct2D 吗?还是 Direct3D?

于 2013-06-04T01:43:11.763 回答