无法创建D2D1设备

时间:2017-05-01 09:28:47

标签: c++ uwp directx direct2d wrl

我正在尝试遵循Microsoft的DirectX and XAML interop官方教程,以便为UWP应用程序创建用于图像基元绘制的C ++运行时库。但是,代码中充斥着错误和拼写错误。我设法调整了部分内容,但是我遇到了运行时错误。

我已按如下方式修改了代码。

Microsoft::WRL::ComPtr<ISurfaceImageSourceNativeWithD2D> m_sisNativeWithD2D;

IInspectable* sisInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(surfaceImageSource);
sisInspectable->QueryInterface(__uuidof(ISurfaceImageSourceNativeWithD2D), (void **)&m_sisNativeWithD2D);

Microsoft::WRL::ComPtr<ID3D11Device> m_d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_d3dContext;
Microsoft::WRL::ComPtr<ID2D1Device> m_d2dDevice;

// Create the DXGI device
D3D11CreateDevice(
    NULL,
    D3D_DRIVER_TYPE_HARDWARE,
    NULL,
    0,
    NULL,
    NULL,
    D3D11_SDK_VERSION,
    &m_d3dDevice,
    NULL,
    &m_d3dContext);

Microsoft::WRL::ComPtr<IDXGIDevice> dxgiDevice;
m_d3dDevice.As(&dxgiDevice);

// To enable multi-threaded access (optional)
Microsoft::WRL::ComPtr<ID3D10Multithread> d3dMultiThread;

m_d3dDevice->QueryInterface(__uuidof(ID3D10Multithread), (void **)&d3dMultiThread);

d3dMultiThread->SetMultithreadProtected(TRUE);

// Create the D2D device
HRESULT d2d1 = D2D1CreateDevice(dxgiDevice.Get(), NULL, &m_d2dDevice);

在调试代码时,我可以看到d2d1句柄正在返回E_INVALIDARG。我尝试传递设备属性而不是NULL并禁用多线程访问,但错误仍然存​​在。此行之前的对象似乎已正确初始化。

可能导致此行为的原因是什么?

修改

VTT的评论有助于构建设备,但似乎设备不合适。我改编的代码如下。

Microsoft::WRL::ComPtr<ISurfaceImageSourceNativeWithD2D> m_sisNativeWithD2D;

IInspectable* sisInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(surfaceImageSource);
sisInspectable->QueryInterface(__uuidof(ISurfaceImageSourceNativeWithD2D), (void **)&m_sisNativeWithD2D);

Microsoft::WRL::ComPtr<ID3D11Device> m_d3dDevice;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_d3dContext;
Microsoft::WRL::ComPtr<ID2D1Device> m_d2dDevice;

D3D_FEATURE_LEVEL array[] = {
    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,
};

D3D_FEATURE_LEVEL selectedFeatureLevel;

// Create the DXGI device
D3D11CreateDevice(
    NULL,
    D3D_DRIVER_TYPE_HARDWARE,
    NULL,
    D3D11_CREATE_DEVICE_BGRA_SUPPORT,
    array,
    sizeof(array)/sizeof(D3D_FEATURE_LEVEL),
    D3D11_SDK_VERSION,
    &m_d3dDevice,
    &selectedFeatureLevel,
    &m_d3dContext);

//Microsoft::WRL::ComPtr<IDXGIDevice> dxgiDevice;
//m_d3dDevice.As(&dxgiDevice);

Microsoft::WRL::ComPtr<IDXGIDevice> dxgiDevice;
auto hr = m_d3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)&dxgiDevice);

// To enable multi-threaded access (optional)
Microsoft::WRL::ComPtr<ID3D10Multithread> d3dMultiThread;
m_d3dDevice->QueryInterface(__uuidof(ID3D10Multithread), (void **)&d3dMultiThread);
d3dMultiThread->SetMultithreadProtected(TRUE);

// Create the D2D device
HRESULT d2d1 = D2D1CreateDevice(dxgiDevice.Get(), NULL, &m_d2dDevice);

// Set the D2D device
m_sisNativeWithD2D->SetDevice(m_d2dDevice.Get());

Microsoft::WRL::ComPtr<ID2D1DeviceContext> drawingContext;

RECT rect;
rect.top = 0;
rect.left = 0;
rect.right = 50;
rect.bottom = 50;

POINT offset;

HRESULT beginDrawHR = m_sisNativeWithD2D->BeginDraw(rect, __uuidof(ID2D1DeviceContext), &drawingContext, &offset);

但是,此处BeginDraw()方法失败并显示E_INVALIDARG。我假设这是因为设备构造不正确。

0 个答案:

没有答案