我正在从一些示例学习DirectX 12,但我无法理解ComPtr.As()方法的作用。
ComPtr<ID3D12Device> device;
ComPtr<ID3D12Device> device2;
// Do Stuff with Device
device.As(&device2); // What does this do?
答案 0 :(得分:1)
您在哪里找到此示例代码?它看起来很腥。这是As
的无意义使用。如果将其扩展到基础QueryInterface
:
hr = device->QueryInterface( IID_PPV_ARGS(device2) );
事实上,您的代码更好地写为:
device2 = device;
通常,您使用As
从现有界面获取新界面。例如,对于Direct3D 11,您将设备创建为Direct3D 11.0接口,然后必须QueryInterface
11.1,11.2,11.3和/或11.4版本才能使用它们:
ComPtr<ID3D11Device> device;
DX::ThrowIfFailed(D3D11CreateDevice(..., device.ReleaseAndGetAddressOf());
ComPtr<ID3D11Device2> device2;
hr = device.As(&device2);
if (FAILED(hr))
// Do whatever handling you do if the system doesn't support 11.2
Direct3D 11的另一个常见用途是处理可能不存在的调试接口:
#ifndef NDEBUG
ComPtr<ID3D11Debug> d3dDebug;
if (SUCCEEDED(device.As(&d3dDebug)))
{
ComPtr<ID3D11InfoQueue> d3dInfoQueue;
if (SUCCEEDED(d3dDebug.As(&d3dInfoQueue)))
{
#ifdef _DEBUG
d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
#endif
D3D11_MESSAGE_ID hide [] =
{
D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS,
// TODO: Add more message IDs here as needed.
};
D3D11_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumIDs = _countof(hide);
filter.DenyList.pIDList = hide;
d3dInfoQueue->AddStorageFilterEntries(&filter);
}
}
#endif
不要忽略从As
返回的HRESULT返回值也很重要。您应该使用SUCCEEDED
或FAILED
宏,或像ThrowIfFailed这样的快速失败。
请参阅ComPtr。