ComPtr.As()有什么作用? [英] What does ComPtr.As() do?
问题描述
我正在从一些例子学习DirectX 12,但我无法理解ComPtr.As()方法是什么。
I am working on learning DirectX 12 from some examples but I am having trouble understanding what does the ComPtr.As() method does.
ComPtr<ID3D12Device> device;
ComPtr<ID3D12Device> device2;
// Do Stuff with Device
device.As(&device2); // What does this do?
推荐答案
您在哪里找到此示例代码?它看起来鱼腥。这是对 As
的无谓使用。如果你将它扩展到底层的 QueryInterface
:
Where did you find this example code? It looks fishy. This is kind of a nonsense use of As
. It's equally silly if you expand it to the underlying QueryInterface
:
hr = device->QueryInterface( IID_PPV_ARGS(device2) );
其实,你的代码最好写成:
In fact, your code would be better written as:
device2 = device;
通常你会使用 As
从现有接口的新接口。例如,使用Direct3D 11,您将设备创建为Direct3D 11.0接口,然后必须使用 QueryInterface
11.1,11.2,11.3和/或11.4版本才能使用它们: / p>
Typically you'd use As
to obtain a new interface from an existing interface. For example with Direct3D 11, you create the device as a Direct3D 11.0 interface and then have to QueryInterface
the 11.1, 11.2, 11.3, and/or 11.4 versions to use them:
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的另一个常见用途是处理可能不存在的调试界面:
Another common use with Direct3D 11 is dealing with the debug interfaces which may not be present:
#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 。
It's also important to not ignore the HRESULT return value that comes back from As
. You should use SUCCEEDED
or FAILED
macro, or a fast-fail like ThrowIfFailed.
请参阅 ComPtr 。
这篇关于ComPtr.As()有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!